blob: 98f05e2b0ecc97da12eb0c48a09f38098966e66b [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 Kivity313dbd42008-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
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001575static inline bool cpu_has_vmx_invvpid_single(void)
1576{
1577 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1578}
1579
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001580static inline bool cpu_has_vmx_invvpid_global(void)
1581{
1582 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1583}
1584
Wanpeng Li08d839c2017-03-23 05:30:08 -07001585static inline bool cpu_has_vmx_invvpid(void)
1586{
1587 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1588}
1589
Gui Jianfeng31299942010-03-15 17:29:09 +08001590static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001591{
Sheng Yang04547152009-04-01 15:52:31 +08001592 return vmcs_config.cpu_based_2nd_exec_ctrl &
1593 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001594}
1595
Gui Jianfeng31299942010-03-15 17:29:09 +08001596static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001597{
1598 return vmcs_config.cpu_based_2nd_exec_ctrl &
1599 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1600}
1601
Gui Jianfeng31299942010-03-15 17:29:09 +08001602static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001603{
1604 return vmcs_config.cpu_based_2nd_exec_ctrl &
1605 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1606}
1607
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001608static inline bool cpu_has_vmx_basic_inout(void)
1609{
1610 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1611}
1612
Paolo Bonzini35754c92015-07-29 12:05:37 +02001613static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001614{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001615 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001616}
1617
Gui Jianfeng31299942010-03-15 17:29:09 +08001618static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001619{
Sheng Yang04547152009-04-01 15:52:31 +08001620 return vmcs_config.cpu_based_2nd_exec_ctrl &
1621 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001622}
1623
Gui Jianfeng31299942010-03-15 17:29:09 +08001624static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001625{
1626 return vmcs_config.cpu_based_2nd_exec_ctrl &
1627 SECONDARY_EXEC_RDTSCP;
1628}
1629
Mao, Junjiead756a12012-07-02 01:18:48 +00001630static inline bool cpu_has_vmx_invpcid(void)
1631{
1632 return vmcs_config.cpu_based_2nd_exec_ctrl &
1633 SECONDARY_EXEC_ENABLE_INVPCID;
1634}
1635
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001636static inline bool cpu_has_virtual_nmis(void)
1637{
1638 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1639}
1640
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001641static inline bool cpu_has_vmx_wbinvd_exit(void)
1642{
1643 return vmcs_config.cpu_based_2nd_exec_ctrl &
1644 SECONDARY_EXEC_WBINVD_EXITING;
1645}
1646
Abel Gordonabc4fc52013-04-18 14:35:25 +03001647static inline bool cpu_has_vmx_shadow_vmcs(void)
1648{
1649 u64 vmx_msr;
1650 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1651 /* check if the cpu supports writing r/o exit information fields */
1652 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1653 return false;
1654
1655 return vmcs_config.cpu_based_2nd_exec_ctrl &
1656 SECONDARY_EXEC_SHADOW_VMCS;
1657}
1658
Kai Huang843e4332015-01-28 10:54:28 +08001659static inline bool cpu_has_vmx_pml(void)
1660{
1661 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1662}
1663
Haozhong Zhang64903d62015-10-20 15:39:09 +08001664static inline bool cpu_has_vmx_tsc_scaling(void)
1665{
1666 return vmcs_config.cpu_based_2nd_exec_ctrl &
1667 SECONDARY_EXEC_TSC_SCALING;
1668}
1669
Bandan Das2a499e42017-08-03 15:54:41 -04001670static inline bool cpu_has_vmx_vmfunc(void)
1671{
1672 return vmcs_config.cpu_based_2nd_exec_ctrl &
1673 SECONDARY_EXEC_ENABLE_VMFUNC;
1674}
1675
Sean Christopherson64f7a112018-04-30 10:01:06 -07001676static bool vmx_umip_emulated(void)
1677{
1678 return vmcs_config.cpu_based_2nd_exec_ctrl &
1679 SECONDARY_EXEC_DESC;
1680}
1681
Sheng Yang04547152009-04-01 15:52:31 +08001682static inline bool report_flexpriority(void)
1683{
1684 return flexpriority_enabled;
1685}
1686
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001687static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1688{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001689 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001690}
1691
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001692static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1693{
1694 return vmcs12->cpu_based_vm_exec_control & bit;
1695}
1696
1697static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1698{
1699 return (vmcs12->cpu_based_vm_exec_control &
1700 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1701 (vmcs12->secondary_vm_exec_control & bit);
1702}
1703
Jan Kiszkaf41245002014-03-07 20:03:13 +01001704static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1705{
1706 return vmcs12->pin_based_vm_exec_control &
1707 PIN_BASED_VMX_PREEMPTION_TIMER;
1708}
1709
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001710static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1711{
1712 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1713}
1714
1715static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1716{
1717 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1718}
1719
Nadav Har'El155a97a2013-08-05 11:07:16 +03001720static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1721{
1722 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1723}
1724
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001725static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1726{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001727 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001728}
1729
Bandan Dasc5f983f2017-05-05 15:25:14 -04001730static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1731{
1732 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1733}
1734
Wincy Vanf2b93282015-02-03 23:56:03 +08001735static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1736{
1737 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1738}
1739
Wanpeng Li5c614b32015-10-13 09:18:36 -07001740static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1741{
1742 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1743}
1744
Wincy Van82f0dd42015-02-03 23:57:18 +08001745static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1746{
1747 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1748}
1749
Wincy Van608406e2015-02-03 23:57:51 +08001750static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1751{
1752 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1753}
1754
Wincy Van705699a2015-02-03 23:58:17 +08001755static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1756{
1757 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1758}
1759
Bandan Das27c42a12017-08-03 15:54:42 -04001760static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1761{
1762 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1763}
1764
Bandan Das41ab9372017-08-03 15:54:43 -04001765static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1766{
1767 return nested_cpu_has_vmfunc(vmcs12) &&
1768 (vmcs12->vm_function_control &
1769 VMX_VMFUNC_EPTP_SWITCHING);
1770}
1771
Jim Mattsonef85b672016-12-12 11:01:37 -08001772static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001773{
1774 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001775 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001776}
1777
Jan Kiszka533558b2014-01-04 18:47:20 +01001778static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1779 u32 exit_intr_info,
1780 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001781static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1782 struct vmcs12 *vmcs12,
1783 u32 reason, unsigned long qualification);
1784
Rusty Russell8b9cf982007-07-30 16:31:43 +10001785static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001786{
1787 int i;
1788
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001789 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001790 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001791 return i;
1792 return -1;
1793}
1794
Sheng Yang2384d2b2008-01-17 15:14:33 +08001795static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1796{
1797 struct {
1798 u64 vpid : 16;
1799 u64 rsvd : 48;
1800 u64 gva;
1801 } operand = { vpid, 0, gva };
1802
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001803 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001804 /* CF==1 or ZF==1 --> rc = -1 */
1805 "; ja 1f ; ud2 ; 1:"
1806 : : "a"(&operand), "c"(ext) : "cc", "memory");
1807}
1808
Sheng Yang14394422008-04-28 12:24:45 +08001809static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1810{
1811 struct {
1812 u64 eptp, gpa;
1813 } operand = {eptp, gpa};
1814
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001815 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001816 /* CF==1 or ZF==1 --> rc = -1 */
1817 "; ja 1f ; ud2 ; 1:\n"
1818 : : "a" (&operand), "c" (ext) : "cc", "memory");
1819}
1820
Avi Kivity26bb0982009-09-07 11:14:12 +03001821static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001822{
1823 int i;
1824
Rusty Russell8b9cf982007-07-30 16:31:43 +10001825 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001826 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001827 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001828 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001829}
1830
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831static void vmcs_clear(struct vmcs *vmcs)
1832{
1833 u64 phys_addr = __pa(vmcs);
1834 u8 error;
1835
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001836 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001837 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838 : "cc", "memory");
1839 if (error)
1840 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1841 vmcs, phys_addr);
1842}
1843
Nadav Har'Eld462b812011-05-24 15:26:10 +03001844static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1845{
1846 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001847 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1848 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001849 loaded_vmcs->cpu = -1;
1850 loaded_vmcs->launched = 0;
1851}
1852
Dongxiao Xu7725b892010-05-11 18:29:38 +08001853static void vmcs_load(struct vmcs *vmcs)
1854{
1855 u64 phys_addr = __pa(vmcs);
1856 u8 error;
1857
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001858 if (static_branch_unlikely(&enable_evmcs))
1859 return evmcs_load(phys_addr);
1860
Dongxiao Xu7725b892010-05-11 18:29:38 +08001861 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001862 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001863 : "cc", "memory");
1864 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001865 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001866 vmcs, phys_addr);
1867}
1868
Dave Young2965faa2015-09-09 15:38:55 -07001869#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001870/*
1871 * This bitmap is used to indicate whether the vmclear
1872 * operation is enabled on all cpus. All disabled by
1873 * default.
1874 */
1875static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1876
1877static inline void crash_enable_local_vmclear(int cpu)
1878{
1879 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1880}
1881
1882static inline void crash_disable_local_vmclear(int cpu)
1883{
1884 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1885}
1886
1887static inline int crash_local_vmclear_enabled(int cpu)
1888{
1889 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1890}
1891
1892static void crash_vmclear_local_loaded_vmcss(void)
1893{
1894 int cpu = raw_smp_processor_id();
1895 struct loaded_vmcs *v;
1896
1897 if (!crash_local_vmclear_enabled(cpu))
1898 return;
1899
1900 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1901 loaded_vmcss_on_cpu_link)
1902 vmcs_clear(v->vmcs);
1903}
1904#else
1905static inline void crash_enable_local_vmclear(int cpu) { }
1906static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001907#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001908
Nadav Har'Eld462b812011-05-24 15:26:10 +03001909static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001911 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001912 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913
Nadav Har'Eld462b812011-05-24 15:26:10 +03001914 if (loaded_vmcs->cpu != cpu)
1915 return; /* vcpu migration can race with cpu offline */
1916 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001918 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001919 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001920
1921 /*
1922 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1923 * is before setting loaded_vmcs->vcpu to -1 which is done in
1924 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1925 * then adds the vmcs into percpu list before it is deleted.
1926 */
1927 smp_wmb();
1928
Nadav Har'Eld462b812011-05-24 15:26:10 +03001929 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001930 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931}
1932
Nadav Har'Eld462b812011-05-24 15:26:10 +03001933static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001934{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001935 int cpu = loaded_vmcs->cpu;
1936
1937 if (cpu != -1)
1938 smp_call_function_single(cpu,
1939 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001940}
1941
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001942static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001943{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001944 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001945 return;
1946
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001947 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001948 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001949}
1950
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001951static inline void vpid_sync_vcpu_global(void)
1952{
1953 if (cpu_has_vmx_invvpid_global())
1954 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1955}
1956
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001957static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001958{
1959 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001960 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001961 else
1962 vpid_sync_vcpu_global();
1963}
1964
Sheng Yang14394422008-04-28 12:24:45 +08001965static inline void ept_sync_global(void)
1966{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001967 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001968}
1969
1970static inline void ept_sync_context(u64 eptp)
1971{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001972 if (cpu_has_vmx_invept_context())
1973 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1974 else
1975 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001976}
1977
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001978static __always_inline void vmcs_check16(unsigned long field)
1979{
1980 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1981 "16-bit accessor invalid for 64-bit field");
1982 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1983 "16-bit accessor invalid for 64-bit high field");
1984 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1985 "16-bit accessor invalid for 32-bit high field");
1986 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1987 "16-bit accessor invalid for natural width field");
1988}
1989
1990static __always_inline void vmcs_check32(unsigned long field)
1991{
1992 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1993 "32-bit accessor invalid for 16-bit field");
1994 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1995 "32-bit accessor invalid for natural width field");
1996}
1997
1998static __always_inline void vmcs_check64(unsigned long field)
1999{
2000 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2001 "64-bit accessor invalid for 16-bit field");
2002 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2003 "64-bit accessor invalid for 64-bit high field");
2004 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2005 "64-bit accessor invalid for 32-bit field");
2006 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2007 "64-bit accessor invalid for natural width field");
2008}
2009
2010static __always_inline void vmcs_checkl(unsigned long field)
2011{
2012 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2013 "Natural width accessor invalid for 16-bit field");
2014 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2015 "Natural width accessor invalid for 64-bit field");
2016 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2017 "Natural width accessor invalid for 64-bit high field");
2018 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2019 "Natural width accessor invalid for 32-bit field");
2020}
2021
2022static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023{
Avi Kivity5e520e62011-05-15 10:13:12 -04002024 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025
Avi Kivity5e520e62011-05-15 10:13:12 -04002026 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2027 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028 return value;
2029}
2030
Avi Kivity96304212011-05-15 10:13:13 -04002031static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002033 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002034 if (static_branch_unlikely(&enable_evmcs))
2035 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002036 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037}
2038
Avi Kivity96304212011-05-15 10:13:13 -04002039static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002041 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002042 if (static_branch_unlikely(&enable_evmcs))
2043 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002044 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045}
2046
Avi Kivity96304212011-05-15 10:13:13 -04002047static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002049 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002050 if (static_branch_unlikely(&enable_evmcs))
2051 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002052#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002053 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002055 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056#endif
2057}
2058
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002059static __always_inline unsigned long vmcs_readl(unsigned long field)
2060{
2061 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002062 if (static_branch_unlikely(&enable_evmcs))
2063 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002064 return __vmcs_readl(field);
2065}
2066
Avi Kivitye52de1b2007-01-05 16:36:56 -08002067static noinline void vmwrite_error(unsigned long field, unsigned long value)
2068{
2069 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2070 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2071 dump_stack();
2072}
2073
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002074static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075{
2076 u8 error;
2077
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002078 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002079 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002080 if (unlikely(error))
2081 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082}
2083
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002084static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002086 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002087 if (static_branch_unlikely(&enable_evmcs))
2088 return evmcs_write16(field, value);
2089
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002090 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091}
2092
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002093static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002095 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002096 if (static_branch_unlikely(&enable_evmcs))
2097 return evmcs_write32(field, value);
2098
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002099 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100}
2101
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002102static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002104 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002105 if (static_branch_unlikely(&enable_evmcs))
2106 return evmcs_write64(field, value);
2107
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002108 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002109#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002111 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112#endif
2113}
2114
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002115static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002116{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002117 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002118 if (static_branch_unlikely(&enable_evmcs))
2119 return evmcs_write64(field, value);
2120
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002121 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002122}
2123
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002124static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002125{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002126 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2127 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002128 if (static_branch_unlikely(&enable_evmcs))
2129 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2130
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002131 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2132}
2133
2134static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2135{
2136 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2137 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002138 if (static_branch_unlikely(&enable_evmcs))
2139 return evmcs_write32(field, evmcs_read32(field) | mask);
2140
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002141 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002142}
2143
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002144static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2145{
2146 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2147}
2148
Gleb Natapov2961e8762013-11-25 15:37:13 +02002149static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2150{
2151 vmcs_write32(VM_ENTRY_CONTROLS, val);
2152 vmx->vm_entry_controls_shadow = val;
2153}
2154
2155static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2156{
2157 if (vmx->vm_entry_controls_shadow != val)
2158 vm_entry_controls_init(vmx, val);
2159}
2160
2161static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2162{
2163 return vmx->vm_entry_controls_shadow;
2164}
2165
2166
2167static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2168{
2169 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2170}
2171
2172static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2173{
2174 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2175}
2176
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002177static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2178{
2179 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2180}
2181
Gleb Natapov2961e8762013-11-25 15:37:13 +02002182static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2183{
2184 vmcs_write32(VM_EXIT_CONTROLS, val);
2185 vmx->vm_exit_controls_shadow = val;
2186}
2187
2188static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2189{
2190 if (vmx->vm_exit_controls_shadow != val)
2191 vm_exit_controls_init(vmx, val);
2192}
2193
2194static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2195{
2196 return vmx->vm_exit_controls_shadow;
2197}
2198
2199
2200static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2201{
2202 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2203}
2204
2205static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2206{
2207 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2208}
2209
Avi Kivity2fb92db2011-04-27 19:42:18 +03002210static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2211{
2212 vmx->segment_cache.bitmask = 0;
2213}
2214
2215static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2216 unsigned field)
2217{
2218 bool ret;
2219 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2220
2221 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2222 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2223 vmx->segment_cache.bitmask = 0;
2224 }
2225 ret = vmx->segment_cache.bitmask & mask;
2226 vmx->segment_cache.bitmask |= mask;
2227 return ret;
2228}
2229
2230static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2231{
2232 u16 *p = &vmx->segment_cache.seg[seg].selector;
2233
2234 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2235 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2236 return *p;
2237}
2238
2239static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2240{
2241 ulong *p = &vmx->segment_cache.seg[seg].base;
2242
2243 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2244 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2245 return *p;
2246}
2247
2248static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2249{
2250 u32 *p = &vmx->segment_cache.seg[seg].limit;
2251
2252 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2253 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2254 return *p;
2255}
2256
2257static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2258{
2259 u32 *p = &vmx->segment_cache.seg[seg].ar;
2260
2261 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2262 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2263 return *p;
2264}
2265
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002266static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2267{
2268 u32 eb;
2269
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002270 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002271 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002272 /*
2273 * Guest access to VMware backdoor ports could legitimately
2274 * trigger #GP because of TSS I/O permission bitmap.
2275 * We intercept those #GP and allow access to them anyway
2276 * as VMware does.
2277 */
2278 if (enable_vmware_backdoor)
2279 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002280 if ((vcpu->guest_debug &
2281 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2282 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2283 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002284 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002285 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002286 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002287 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002288
2289 /* When we are running a nested L2 guest and L1 specified for it a
2290 * certain exception bitmap, we must trap the same exceptions and pass
2291 * them to L1. When running L2, we will only handle the exceptions
2292 * specified above if L1 did not want them.
2293 */
2294 if (is_guest_mode(vcpu))
2295 eb |= get_vmcs12(vcpu)->exception_bitmap;
2296
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002297 vmcs_write32(EXCEPTION_BITMAP, eb);
2298}
2299
Ashok Raj15d45072018-02-01 22:59:43 +01002300/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002301 * Check if MSR is intercepted for currently loaded MSR bitmap.
2302 */
2303static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2304{
2305 unsigned long *msr_bitmap;
2306 int f = sizeof(unsigned long);
2307
2308 if (!cpu_has_vmx_msr_bitmap())
2309 return true;
2310
2311 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2312
2313 if (msr <= 0x1fff) {
2314 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2315 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2316 msr &= 0x1fff;
2317 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2318 }
2319
2320 return true;
2321}
2322
2323/*
Ashok Raj15d45072018-02-01 22:59:43 +01002324 * Check if MSR is intercepted for L01 MSR bitmap.
2325 */
2326static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2327{
2328 unsigned long *msr_bitmap;
2329 int f = sizeof(unsigned long);
2330
2331 if (!cpu_has_vmx_msr_bitmap())
2332 return true;
2333
2334 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2335
2336 if (msr <= 0x1fff) {
2337 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2338 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2339 msr &= 0x1fff;
2340 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2341 }
2342
2343 return true;
2344}
2345
Gleb Natapov2961e8762013-11-25 15:37:13 +02002346static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2347 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002348{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002349 vm_entry_controls_clearbit(vmx, entry);
2350 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002351}
2352
Avi Kivity61d2ef22010-04-28 16:40:38 +03002353static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2354{
2355 unsigned i;
2356 struct msr_autoload *m = &vmx->msr_autoload;
2357
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002358 switch (msr) {
2359 case MSR_EFER:
2360 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002361 clear_atomic_switch_msr_special(vmx,
2362 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002363 VM_EXIT_LOAD_IA32_EFER);
2364 return;
2365 }
2366 break;
2367 case MSR_CORE_PERF_GLOBAL_CTRL:
2368 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002369 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002370 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2371 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2372 return;
2373 }
2374 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002375 }
2376
Avi Kivity61d2ef22010-04-28 16:40:38 +03002377 for (i = 0; i < m->nr; ++i)
2378 if (m->guest[i].index == msr)
2379 break;
2380
2381 if (i == m->nr)
2382 return;
2383 --m->nr;
2384 m->guest[i] = m->guest[m->nr];
2385 m->host[i] = m->host[m->nr];
2386 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2387 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2388}
2389
Gleb Natapov2961e8762013-11-25 15:37:13 +02002390static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2391 unsigned long entry, unsigned long exit,
2392 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2393 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002394{
2395 vmcs_write64(guest_val_vmcs, guest_val);
2396 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002397 vm_entry_controls_setbit(vmx, entry);
2398 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002399}
2400
Avi Kivity61d2ef22010-04-28 16:40:38 +03002401static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2402 u64 guest_val, u64 host_val)
2403{
2404 unsigned i;
2405 struct msr_autoload *m = &vmx->msr_autoload;
2406
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002407 switch (msr) {
2408 case MSR_EFER:
2409 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002410 add_atomic_switch_msr_special(vmx,
2411 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002412 VM_EXIT_LOAD_IA32_EFER,
2413 GUEST_IA32_EFER,
2414 HOST_IA32_EFER,
2415 guest_val, host_val);
2416 return;
2417 }
2418 break;
2419 case MSR_CORE_PERF_GLOBAL_CTRL:
2420 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002421 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002422 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2423 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2424 GUEST_IA32_PERF_GLOBAL_CTRL,
2425 HOST_IA32_PERF_GLOBAL_CTRL,
2426 guest_val, host_val);
2427 return;
2428 }
2429 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002430 case MSR_IA32_PEBS_ENABLE:
2431 /* PEBS needs a quiescent period after being disabled (to write
2432 * a record). Disabling PEBS through VMX MSR swapping doesn't
2433 * provide that period, so a CPU could write host's record into
2434 * guest's memory.
2435 */
2436 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002437 }
2438
Avi Kivity61d2ef22010-04-28 16:40:38 +03002439 for (i = 0; i < m->nr; ++i)
2440 if (m->guest[i].index == msr)
2441 break;
2442
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002443 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002444 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002445 "Can't add msr %x\n", msr);
2446 return;
2447 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002448 ++m->nr;
2449 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2450 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2451 }
2452
2453 m->guest[i].index = msr;
2454 m->guest[i].value = guest_val;
2455 m->host[i].index = msr;
2456 m->host[i].value = host_val;
2457}
2458
Avi Kivity92c0d902009-10-29 11:00:16 +02002459static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002460{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002461 u64 guest_efer = vmx->vcpu.arch.efer;
2462 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002463
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002464 if (!enable_ept) {
2465 /*
2466 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2467 * host CPUID is more efficient than testing guest CPUID
2468 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2469 */
2470 if (boot_cpu_has(X86_FEATURE_SMEP))
2471 guest_efer |= EFER_NX;
2472 else if (!(guest_efer & EFER_NX))
2473 ignore_bits |= EFER_NX;
2474 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002475
Avi Kivity51c6cf62007-08-29 03:48:05 +03002476 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002477 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002478 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002479 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002480#ifdef CONFIG_X86_64
2481 ignore_bits |= EFER_LMA | EFER_LME;
2482 /* SCE is meaningful only in long mode on Intel */
2483 if (guest_efer & EFER_LMA)
2484 ignore_bits &= ~(u64)EFER_SCE;
2485#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002486
2487 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002488
2489 /*
2490 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2491 * On CPUs that support "load IA32_EFER", always switch EFER
2492 * atomically, since it's faster than switching it manually.
2493 */
2494 if (cpu_has_load_ia32_efer ||
2495 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002496 if (!(guest_efer & EFER_LMA))
2497 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002498 if (guest_efer != host_efer)
2499 add_atomic_switch_msr(vmx, MSR_EFER,
2500 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002501 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002502 } else {
2503 guest_efer &= ~ignore_bits;
2504 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002505
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002506 vmx->guest_msrs[efer_offset].data = guest_efer;
2507 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2508
2509 return true;
2510 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002511}
2512
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002513#ifdef CONFIG_X86_32
2514/*
2515 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2516 * VMCS rather than the segment table. KVM uses this helper to figure
2517 * out the current bases to poke them into the VMCS before entry.
2518 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002519static unsigned long segment_base(u16 selector)
2520{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002521 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002522 unsigned long v;
2523
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002524 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002525 return 0;
2526
Thomas Garnier45fc8752017-03-14 10:05:08 -07002527 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002528
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002529 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002530 u16 ldt_selector = kvm_read_ldt();
2531
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002532 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002533 return 0;
2534
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002535 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002536 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002537 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002538 return v;
2539}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002540#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002541
Avi Kivity04d2cc72007-09-10 18:10:54 +03002542static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002543{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002544 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002545#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002546 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002547#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002548 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002549
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002550 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002551 return;
2552
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002553 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002554 /*
2555 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2556 * allow segment selectors with cpl > 0 or ti == 1.
2557 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002558 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002559 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002560
2561#ifdef CONFIG_X86_64
2562 save_fsgs_for_kvm();
2563 vmx->host_state.fs_sel = current->thread.fsindex;
2564 vmx->host_state.gs_sel = current->thread.gsindex;
2565#else
Avi Kivity9581d442010-10-19 16:46:55 +02002566 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002567 savesegment(gs, vmx->host_state.gs_sel);
2568#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002569 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002570 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002571 vmx->host_state.fs_reload_needed = 0;
2572 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002573 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002574 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002575 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002576 if (!(vmx->host_state.gs_sel & 7))
2577 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002578 else {
2579 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002580 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002581 }
2582
2583#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002584 savesegment(ds, vmx->host_state.ds_sel);
2585 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002586
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002587 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002588 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002589
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002590 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002591 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002592 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002593#else
2594 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2595 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2596#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002597 if (boot_cpu_has(X86_FEATURE_MPX))
2598 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002599 for (i = 0; i < vmx->save_nmsrs; ++i)
2600 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002601 vmx->guest_msrs[i].data,
2602 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002603}
2604
Avi Kivitya9b21b62008-06-24 11:48:49 +03002605static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002606{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002607 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002608 return;
2609
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002610 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002611 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002612#ifdef CONFIG_X86_64
2613 if (is_long_mode(&vmx->vcpu))
2614 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2615#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002616 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002617 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002618#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002619 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002620#else
2621 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002622#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002623 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002624 if (vmx->host_state.fs_reload_needed)
2625 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002626#ifdef CONFIG_X86_64
2627 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2628 loadsegment(ds, vmx->host_state.ds_sel);
2629 loadsegment(es, vmx->host_state.es_sel);
2630 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002631#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002632 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002633#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002634 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002635#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002636 if (vmx->host_state.msr_host_bndcfgs)
2637 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002638 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002639}
2640
Avi Kivitya9b21b62008-06-24 11:48:49 +03002641static void vmx_load_host_state(struct vcpu_vmx *vmx)
2642{
2643 preempt_disable();
2644 __vmx_load_host_state(vmx);
2645 preempt_enable();
2646}
2647
Feng Wu28b835d2015-09-18 22:29:54 +08002648static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2649{
2650 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2651 struct pi_desc old, new;
2652 unsigned int dest;
2653
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002654 /*
2655 * In case of hot-plug or hot-unplug, we may have to undo
2656 * vmx_vcpu_pi_put even if there is no assigned device. And we
2657 * always keep PI.NDST up to date for simplicity: it makes the
2658 * code easier, and CPU migration is not a fast path.
2659 */
2660 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002661 return;
2662
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002663 /*
2664 * First handle the simple case where no cmpxchg is necessary; just
2665 * allow posting non-urgent interrupts.
2666 *
2667 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2668 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2669 * expects the VCPU to be on the blocked_vcpu_list that matches
2670 * PI.NDST.
2671 */
2672 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2673 vcpu->cpu == cpu) {
2674 pi_clear_sn(pi_desc);
2675 return;
2676 }
2677
2678 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002679 do {
2680 old.control = new.control = pi_desc->control;
2681
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002682 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002683
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002684 if (x2apic_enabled())
2685 new.ndst = dest;
2686 else
2687 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002688
Feng Wu28b835d2015-09-18 22:29:54 +08002689 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002690 } while (cmpxchg64(&pi_desc->control, old.control,
2691 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002692}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002693
Peter Feinerc95ba922016-08-17 09:36:47 -07002694static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2695{
2696 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2697 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2698}
2699
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700/*
2701 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2702 * vcpu mutex is already taken.
2703 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002704static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002705{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002706 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002707 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002709 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002710 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002711 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002712 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002713
2714 /*
2715 * Read loaded_vmcs->cpu should be before fetching
2716 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2717 * See the comments in __loaded_vmcs_clear().
2718 */
2719 smp_rmb();
2720
Nadav Har'Eld462b812011-05-24 15:26:10 +03002721 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2722 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002723 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002724 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002725 }
2726
2727 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2728 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2729 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002730 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002731 }
2732
2733 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002734 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002735 unsigned long sysenter_esp;
2736
2737 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002738
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 /*
2740 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002741 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002743 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002744 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002745 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002747 /*
2748 * VM exits change the host TR limit to 0x67 after a VM
2749 * exit. This is okay, since 0x67 covers everything except
2750 * the IO bitmap and have have code to handle the IO bitmap
2751 * being lost after a VM exit.
2752 */
2753 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2754
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2756 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002757
Nadav Har'Eld462b812011-05-24 15:26:10 +03002758 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759 }
Feng Wu28b835d2015-09-18 22:29:54 +08002760
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002761 /* Setup TSC multiplier */
2762 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002763 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2764 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002765
Feng Wu28b835d2015-09-18 22:29:54 +08002766 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002767 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002768 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002769}
2770
2771static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2772{
2773 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2774
2775 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002776 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2777 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002778 return;
2779
2780 /* Set SN when the vCPU is preempted */
2781 if (vcpu->preempted)
2782 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783}
2784
2785static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2786{
Feng Wu28b835d2015-09-18 22:29:54 +08002787 vmx_vcpu_pi_put(vcpu);
2788
Avi Kivitya9b21b62008-06-24 11:48:49 +03002789 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790}
2791
Wanpeng Lif244dee2017-07-20 01:11:54 -07002792static bool emulation_required(struct kvm_vcpu *vcpu)
2793{
2794 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2795}
2796
Avi Kivityedcafe32009-12-30 18:07:40 +02002797static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2798
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002799/*
2800 * Return the cr0 value that a nested guest would read. This is a combination
2801 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2802 * its hypervisor (cr0_read_shadow).
2803 */
2804static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2805{
2806 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2807 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2808}
2809static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2810{
2811 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2812 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2813}
2814
Avi Kivity6aa8b732006-12-10 02:21:36 -08002815static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2816{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002817 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002818
Avi Kivity6de12732011-03-07 12:51:22 +02002819 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2820 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2821 rflags = vmcs_readl(GUEST_RFLAGS);
2822 if (to_vmx(vcpu)->rmode.vm86_active) {
2823 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2824 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2825 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2826 }
2827 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002828 }
Avi Kivity6de12732011-03-07 12:51:22 +02002829 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830}
2831
2832static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2833{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002834 unsigned long old_rflags = vmx_get_rflags(vcpu);
2835
Avi Kivity6de12732011-03-07 12:51:22 +02002836 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2837 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002838 if (to_vmx(vcpu)->rmode.vm86_active) {
2839 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002840 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002841 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002843
2844 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2845 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846}
2847
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002848static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002849{
2850 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2851 int ret = 0;
2852
2853 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002854 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002855 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002856 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002857
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002858 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002859}
2860
2861static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2862{
2863 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2864 u32 interruptibility = interruptibility_old;
2865
2866 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2867
Jan Kiszka48005f62010-02-19 19:38:07 +01002868 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002869 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002870 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002871 interruptibility |= GUEST_INTR_STATE_STI;
2872
2873 if ((interruptibility != interruptibility_old))
2874 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2875}
2876
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2878{
2879 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002881 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002883 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884
Glauber Costa2809f5d2009-05-12 16:21:05 -04002885 /* skipping an emulated instruction also counts */
2886 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887}
2888
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002889static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2890 unsigned long exit_qual)
2891{
2892 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2893 unsigned int nr = vcpu->arch.exception.nr;
2894 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2895
2896 if (vcpu->arch.exception.has_error_code) {
2897 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2898 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2899 }
2900
2901 if (kvm_exception_is_soft(nr))
2902 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2903 else
2904 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2905
2906 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2907 vmx_get_nmi_mask(vcpu))
2908 intr_info |= INTR_INFO_UNBLOCK_NMI;
2909
2910 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2911}
2912
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002913/*
2914 * KVM wants to inject page-faults which it got to the guest. This function
2915 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002916 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002917static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002918{
2919 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002920 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002921
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002922 if (nr == PF_VECTOR) {
2923 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002924 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002925 return 1;
2926 }
2927 /*
2928 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2929 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2930 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2931 * can be written only when inject_pending_event runs. This should be
2932 * conditional on a new capability---if the capability is disabled,
2933 * kvm_multiple_exception would write the ancillary information to
2934 * CR2 or DR6, for backwards ABI-compatibility.
2935 */
2936 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2937 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002938 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002939 return 1;
2940 }
2941 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002942 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002943 if (nr == DB_VECTOR)
2944 *exit_qual = vcpu->arch.dr6;
2945 else
2946 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002947 return 1;
2948 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002949 }
2950
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002951 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002952}
2953
Wanpeng Licaa057a2018-03-12 04:53:03 -07002954static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2955{
2956 /*
2957 * Ensure that we clear the HLT state in the VMCS. We don't need to
2958 * explicitly skip the instruction because if the HLT state is set,
2959 * then the instruction is already executing and RIP has already been
2960 * advanced.
2961 */
2962 if (kvm_hlt_in_guest(vcpu->kvm) &&
2963 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2964 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2965}
2966
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002967static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002968{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002969 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002970 unsigned nr = vcpu->arch.exception.nr;
2971 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002972 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002973 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002974
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002975 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002976 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002977 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2978 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002979
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002980 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002981 int inc_eip = 0;
2982 if (kvm_exception_is_soft(nr))
2983 inc_eip = vcpu->arch.event_exit_inst_len;
2984 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002985 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002986 return;
2987 }
2988
Sean Christophersonadd5ff72018-03-23 09:34:00 -07002989 WARN_ON_ONCE(vmx->emulation_required);
2990
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002991 if (kvm_exception_is_soft(nr)) {
2992 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2993 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002994 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2995 } else
2996 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2997
2998 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002999
3000 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003001}
3002
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003003static bool vmx_rdtscp_supported(void)
3004{
3005 return cpu_has_vmx_rdtscp();
3006}
3007
Mao, Junjiead756a12012-07-02 01:18:48 +00003008static bool vmx_invpcid_supported(void)
3009{
3010 return cpu_has_vmx_invpcid() && enable_ept;
3011}
3012
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013/*
Eddie Donga75beee2007-05-17 18:55:15 +03003014 * Swap MSR entry in host/guest MSR entry array.
3015 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003016static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003017{
Avi Kivity26bb0982009-09-07 11:14:12 +03003018 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003019
3020 tmp = vmx->guest_msrs[to];
3021 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3022 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003023}
3024
3025/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003026 * Set up the vmcs to automatically save and restore system
3027 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3028 * mode, as fiddling with msrs is very expensive.
3029 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003030static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003031{
Avi Kivity26bb0982009-09-07 11:14:12 +03003032 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003033
Eddie Donga75beee2007-05-17 18:55:15 +03003034 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003035#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003036 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003037 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003038 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003039 move_msr_up(vmx, index, save_nmsrs++);
3040 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003041 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003042 move_msr_up(vmx, index, save_nmsrs++);
3043 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003044 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003045 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003046 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003047 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003048 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003049 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003050 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003051 * if efer.sce is enabled.
3052 */
Brian Gerst8c065852010-07-17 09:03:26 -04003053 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003054 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003055 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003056 }
Eddie Donga75beee2007-05-17 18:55:15 +03003057#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003058 index = __find_msr_index(vmx, MSR_EFER);
3059 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003060 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003061
Avi Kivity26bb0982009-09-07 11:14:12 +03003062 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003063
Yang Zhang8d146952013-01-25 10:18:50 +08003064 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003065 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003066}
3067
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003068static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003070 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003071
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003072 if (is_guest_mode(vcpu) &&
3073 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3074 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3075
3076 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077}
3078
3079/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003080 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003082static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003084 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003085 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003086 * We're here if L1 chose not to trap WRMSR to TSC. According
3087 * to the spec, this should set L1's TSC; The offset that L1
3088 * set for L2 remains unchanged, and still needs to be added
3089 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003090 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003091 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003092 /* recalculate vmcs02.TSC_OFFSET: */
3093 vmcs12 = get_vmcs12(vcpu);
3094 vmcs_write64(TSC_OFFSET, offset +
3095 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3096 vmcs12->tsc_offset : 0));
3097 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003098 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3099 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003100 vmcs_write64(TSC_OFFSET, offset);
3101 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102}
3103
Nadav Har'El801d3422011-05-25 23:02:23 +03003104/*
3105 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3106 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3107 * all guests if the "nested" module option is off, and can also be disabled
3108 * for a single guest by disabling its VMX cpuid bit.
3109 */
3110static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3111{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003112 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003113}
3114
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003116 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3117 * returned for the various VMX controls MSRs when nested VMX is enabled.
3118 * The same values should also be used to verify that vmcs12 control fields are
3119 * valid during nested entry from L1 to L2.
3120 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3121 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3122 * bit in the high half is on if the corresponding bit in the control field
3123 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003124 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003125static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003126{
Paolo Bonzini13893092018-02-26 13:40:09 +01003127 if (!nested) {
3128 memset(msrs, 0, sizeof(*msrs));
3129 return;
3130 }
3131
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003132 /*
3133 * Note that as a general rule, the high half of the MSRs (bits in
3134 * the control fields which may be 1) should be initialized by the
3135 * intersection of the underlying hardware's MSR (i.e., features which
3136 * can be supported) and the list of features we want to expose -
3137 * because they are known to be properly supported in our code.
3138 * Also, usually, the low half of the MSRs (bits which must be 1) can
3139 * be set to 0, meaning that L1 may turn off any of these bits. The
3140 * reason is that if one of these bits is necessary, it will appear
3141 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3142 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003143 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003144 * These rules have exceptions below.
3145 */
3146
3147 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003148 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003149 msrs->pinbased_ctls_low,
3150 msrs->pinbased_ctls_high);
3151 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003152 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003153 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003154 PIN_BASED_EXT_INTR_MASK |
3155 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003156 PIN_BASED_VIRTUAL_NMIS |
3157 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003158 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003159 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003160 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003161
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003162 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003163 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003164 msrs->exit_ctls_low,
3165 msrs->exit_ctls_high);
3166 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003167 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003168
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003169 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003170#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003171 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003172#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003173 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003174 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003175 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003176 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003177 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3178
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003179 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003180 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003181
Jan Kiszka2996fca2014-06-16 13:59:43 +02003182 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003183 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003184
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003185 /* entry controls */
3186 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003187 msrs->entry_ctls_low,
3188 msrs->entry_ctls_high);
3189 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003190 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003191 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003192#ifdef CONFIG_X86_64
3193 VM_ENTRY_IA32E_MODE |
3194#endif
3195 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003196 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003197 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003198 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003199 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003200
Jan Kiszka2996fca2014-06-16 13:59:43 +02003201 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003202 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003203
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003204 /* cpu-based controls */
3205 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003206 msrs->procbased_ctls_low,
3207 msrs->procbased_ctls_high);
3208 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003209 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003210 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003211 CPU_BASED_VIRTUAL_INTR_PENDING |
3212 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003213 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3214 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3215 CPU_BASED_CR3_STORE_EXITING |
3216#ifdef CONFIG_X86_64
3217 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3218#endif
3219 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003220 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3221 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3222 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3223 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003224 /*
3225 * We can allow some features even when not supported by the
3226 * hardware. For example, L1 can specify an MSR bitmap - and we
3227 * can use it to avoid exits to L1 - even when L0 runs L2
3228 * without MSR bitmaps.
3229 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003230 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003231 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003232 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003233
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003234 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003235 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003236 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3237
Paolo Bonzini80154d72017-08-24 13:55:35 +02003238 /*
3239 * secondary cpu-based controls. Do not include those that
3240 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3241 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003242 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003243 msrs->secondary_ctls_low,
3244 msrs->secondary_ctls_high);
3245 msrs->secondary_ctls_low = 0;
3246 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003247 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003248 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003249 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003250 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003251 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003252 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003253
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003254 if (enable_ept) {
3255 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003256 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003257 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003258 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003259 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003260 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003261 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003262 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003263 msrs->ept_caps &= vmx_capability.ept;
3264 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003265 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3266 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003267 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003268 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003269 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003270 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003271 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003272 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003273
Bandan Das27c42a12017-08-03 15:54:42 -04003274 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003275 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003276 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003277 /*
3278 * Advertise EPTP switching unconditionally
3279 * since we emulate it
3280 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003281 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003282 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003283 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003284 }
3285
Paolo Bonzinief697a72016-03-18 16:58:38 +01003286 /*
3287 * Old versions of KVM use the single-context version without
3288 * checking for support, so declare that it is supported even
3289 * though it is treated as global context. The alternative is
3290 * not failing the single-context invvpid, and it is worse.
3291 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003292 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003293 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003294 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003295 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003296 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003297 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003298
Radim Krčmář0790ec12015-03-17 14:02:32 +01003299 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003300 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003301 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3302
Jan Kiszkac18911a2013-03-13 16:06:41 +01003303 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003304 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003305 msrs->misc_low,
3306 msrs->misc_high);
3307 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3308 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003309 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003310 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003311 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003312
3313 /*
3314 * This MSR reports some information about VMX support. We
3315 * should return information about the VMX we emulate for the
3316 * guest, and the VMCS structure we give it - not about the
3317 * VMX support of the underlying hardware.
3318 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003319 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003320 VMCS12_REVISION |
3321 VMX_BASIC_TRUE_CTLS |
3322 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3323 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3324
3325 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003326 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003327
3328 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003329 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003330 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3331 * We picked the standard core2 setting.
3332 */
3333#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3334#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003335 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3336 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003337
3338 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003339 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3340 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003341
3342 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003343 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003344}
3345
David Matlack38991522016-11-29 18:14:08 -08003346/*
3347 * if fixed0[i] == 1: val[i] must be 1
3348 * if fixed1[i] == 0: val[i] must be 0
3349 */
3350static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3351{
3352 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003353}
3354
3355static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3356{
David Matlack38991522016-11-29 18:14:08 -08003357 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003358}
3359
3360static inline u64 vmx_control_msr(u32 low, u32 high)
3361{
3362 return low | ((u64)high << 32);
3363}
3364
David Matlack62cc6b9d2016-11-29 18:14:07 -08003365static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3366{
3367 superset &= mask;
3368 subset &= mask;
3369
3370 return (superset | subset) == superset;
3371}
3372
3373static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3374{
3375 const u64 feature_and_reserved =
3376 /* feature (except bit 48; see below) */
3377 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3378 /* reserved */
3379 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003380 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003381
3382 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3383 return -EINVAL;
3384
3385 /*
3386 * KVM does not emulate a version of VMX that constrains physical
3387 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3388 */
3389 if (data & BIT_ULL(48))
3390 return -EINVAL;
3391
3392 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3393 vmx_basic_vmcs_revision_id(data))
3394 return -EINVAL;
3395
3396 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3397 return -EINVAL;
3398
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003399 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003400 return 0;
3401}
3402
3403static int
3404vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3405{
3406 u64 supported;
3407 u32 *lowp, *highp;
3408
3409 switch (msr_index) {
3410 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003411 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3412 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003413 break;
3414 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003415 lowp = &vmx->nested.msrs.procbased_ctls_low;
3416 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003417 break;
3418 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003419 lowp = &vmx->nested.msrs.exit_ctls_low;
3420 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003421 break;
3422 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003423 lowp = &vmx->nested.msrs.entry_ctls_low;
3424 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003425 break;
3426 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003427 lowp = &vmx->nested.msrs.secondary_ctls_low;
3428 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003429 break;
3430 default:
3431 BUG();
3432 }
3433
3434 supported = vmx_control_msr(*lowp, *highp);
3435
3436 /* Check must-be-1 bits are still 1. */
3437 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3438 return -EINVAL;
3439
3440 /* Check must-be-0 bits are still 0. */
3441 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3442 return -EINVAL;
3443
3444 *lowp = data;
3445 *highp = data >> 32;
3446 return 0;
3447}
3448
3449static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3450{
3451 const u64 feature_and_reserved_bits =
3452 /* feature */
3453 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3454 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3455 /* reserved */
3456 GENMASK_ULL(13, 9) | BIT_ULL(31);
3457 u64 vmx_misc;
3458
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003459 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3460 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003461
3462 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3463 return -EINVAL;
3464
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003465 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003466 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3467 vmx_misc_preemption_timer_rate(data) !=
3468 vmx_misc_preemption_timer_rate(vmx_misc))
3469 return -EINVAL;
3470
3471 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3472 return -EINVAL;
3473
3474 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3475 return -EINVAL;
3476
3477 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3478 return -EINVAL;
3479
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003480 vmx->nested.msrs.misc_low = data;
3481 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003482 return 0;
3483}
3484
3485static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3486{
3487 u64 vmx_ept_vpid_cap;
3488
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003489 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3490 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003491
3492 /* Every bit is either reserved or a feature bit. */
3493 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3494 return -EINVAL;
3495
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003496 vmx->nested.msrs.ept_caps = data;
3497 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003498 return 0;
3499}
3500
3501static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3502{
3503 u64 *msr;
3504
3505 switch (msr_index) {
3506 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003507 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003508 break;
3509 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003511 break;
3512 default:
3513 BUG();
3514 }
3515
3516 /*
3517 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3518 * must be 1 in the restored value.
3519 */
3520 if (!is_bitwise_subset(data, *msr, -1ULL))
3521 return -EINVAL;
3522
3523 *msr = data;
3524 return 0;
3525}
3526
3527/*
3528 * Called when userspace is restoring VMX MSRs.
3529 *
3530 * Returns 0 on success, non-0 otherwise.
3531 */
3532static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3533{
3534 struct vcpu_vmx *vmx = to_vmx(vcpu);
3535
3536 switch (msr_index) {
3537 case MSR_IA32_VMX_BASIC:
3538 return vmx_restore_vmx_basic(vmx, data);
3539 case MSR_IA32_VMX_PINBASED_CTLS:
3540 case MSR_IA32_VMX_PROCBASED_CTLS:
3541 case MSR_IA32_VMX_EXIT_CTLS:
3542 case MSR_IA32_VMX_ENTRY_CTLS:
3543 /*
3544 * The "non-true" VMX capability MSRs are generated from the
3545 * "true" MSRs, so we do not support restoring them directly.
3546 *
3547 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3548 * should restore the "true" MSRs with the must-be-1 bits
3549 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3550 * DEFAULT SETTINGS".
3551 */
3552 return -EINVAL;
3553 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3554 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3555 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3556 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3557 case MSR_IA32_VMX_PROCBASED_CTLS2:
3558 return vmx_restore_control_msr(vmx, msr_index, data);
3559 case MSR_IA32_VMX_MISC:
3560 return vmx_restore_vmx_misc(vmx, data);
3561 case MSR_IA32_VMX_CR0_FIXED0:
3562 case MSR_IA32_VMX_CR4_FIXED0:
3563 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3564 case MSR_IA32_VMX_CR0_FIXED1:
3565 case MSR_IA32_VMX_CR4_FIXED1:
3566 /*
3567 * These MSRs are generated based on the vCPU's CPUID, so we
3568 * do not support restoring them directly.
3569 */
3570 return -EINVAL;
3571 case MSR_IA32_VMX_EPT_VPID_CAP:
3572 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3573 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003574 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003575 return 0;
3576 default:
3577 /*
3578 * The rest of the VMX capability MSRs do not support restore.
3579 */
3580 return -EINVAL;
3581 }
3582}
3583
Jan Kiszkacae50132014-01-04 18:47:22 +01003584/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003585static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003586{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003587 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003588 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003589 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003590 break;
3591 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3592 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003593 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003594 msrs->pinbased_ctls_low,
3595 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003596 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3597 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003598 break;
3599 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3600 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003601 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003602 msrs->procbased_ctls_low,
3603 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003604 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3605 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003606 break;
3607 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3608 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003609 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 msrs->exit_ctls_low,
3611 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003612 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3613 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003614 break;
3615 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3616 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003617 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003618 msrs->entry_ctls_low,
3619 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003620 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3621 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003622 break;
3623 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003624 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003625 msrs->misc_low,
3626 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003627 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003628 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003629 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003630 break;
3631 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003632 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003633 break;
3634 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003635 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003636 break;
3637 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003638 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003639 break;
3640 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003641 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003642 break;
3643 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003644 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003645 msrs->secondary_ctls_low,
3646 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003647 break;
3648 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 *pdata = msrs->ept_caps |
3650 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003651 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003652 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003653 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003654 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003655 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003656 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003657 }
3658
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003659 return 0;
3660}
3661
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003662static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3663 uint64_t val)
3664{
3665 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3666
3667 return !(val & ~valid_bits);
3668}
3669
Tom Lendacky801e4592018-02-21 13:39:51 -06003670static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3671{
Paolo Bonzini13893092018-02-26 13:40:09 +01003672 switch (msr->index) {
3673 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3674 if (!nested)
3675 return 1;
3676 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3677 default:
3678 return 1;
3679 }
3680
3681 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003682}
3683
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003684/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003685 * Reads an msr value (of 'msr_index') into 'pdata'.
3686 * Returns 0 on success, non-0 otherwise.
3687 * Assumes vcpu_load() was already called.
3688 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003689static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003690{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003691 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003692 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003693
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003694 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003695#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003697 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003698 break;
3699 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003700 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003701 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003702 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003703 vmx_load_host_state(vmx);
3704 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003705 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003706#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003707 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003708 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003709 case MSR_IA32_SPEC_CTRL:
3710 if (!msr_info->host_initiated &&
3711 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3712 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3713 return 1;
3714
3715 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3716 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003717 case MSR_IA32_ARCH_CAPABILITIES:
3718 if (!msr_info->host_initiated &&
3719 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3720 return 1;
3721 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3722 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003723 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003724 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003725 break;
3726 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003727 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728 break;
3729 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003730 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003732 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003733 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003734 (!msr_info->host_initiated &&
3735 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003736 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003737 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003738 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003739 case MSR_IA32_MCG_EXT_CTL:
3740 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003741 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003742 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003743 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003744 msr_info->data = vcpu->arch.mcg_ext_ctl;
3745 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003746 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003747 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003748 break;
3749 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3750 if (!nested_vmx_allowed(vcpu))
3751 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003752 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3753 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003754 case MSR_IA32_XSS:
3755 if (!vmx_xsaves_supported())
3756 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003757 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003758 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003759 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003760 if (!msr_info->host_initiated &&
3761 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003762 return 1;
3763 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003765 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003766 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003767 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003768 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003769 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003770 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771 }
3772
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773 return 0;
3774}
3775
Jan Kiszkacae50132014-01-04 18:47:22 +01003776static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3777
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778/*
3779 * Writes msr value into into the appropriate "register".
3780 * Returns 0 on success, non-0 otherwise.
3781 * Assumes vcpu_load() was already called.
3782 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003783static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003785 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003786 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003787 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003788 u32 msr_index = msr_info->index;
3789 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003790
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003792 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003793 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003794 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003795#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003797 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798 vmcs_writel(GUEST_FS_BASE, data);
3799 break;
3800 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003801 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802 vmcs_writel(GUEST_GS_BASE, data);
3803 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003804 case MSR_KERNEL_GS_BASE:
3805 vmx_load_host_state(vmx);
3806 vmx->msr_guest_kernel_gs_base = data;
3807 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003808#endif
3809 case MSR_IA32_SYSENTER_CS:
3810 vmcs_write32(GUEST_SYSENTER_CS, data);
3811 break;
3812 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003813 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003814 break;
3815 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003816 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003817 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003818 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003819 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003820 (!msr_info->host_initiated &&
3821 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003822 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003823 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003824 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003826 vmcs_write64(GUEST_BNDCFGS, data);
3827 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003828 case MSR_IA32_SPEC_CTRL:
3829 if (!msr_info->host_initiated &&
3830 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3831 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3832 return 1;
3833
3834 /* The STIBP bit doesn't fault even if it's not advertised */
3835 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3836 return 1;
3837
3838 vmx->spec_ctrl = data;
3839
3840 if (!data)
3841 break;
3842
3843 /*
3844 * For non-nested:
3845 * When it's written (to non-zero) for the first time, pass
3846 * it through.
3847 *
3848 * For nested:
3849 * The handling of the MSR bitmap for L2 guests is done in
3850 * nested_vmx_merge_msr_bitmap. We should not touch the
3851 * vmcs02.msr_bitmap here since it gets completely overwritten
3852 * in the merging. We update the vmcs01 here for L1 as well
3853 * since it will end up touching the MSR anyway now.
3854 */
3855 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3856 MSR_IA32_SPEC_CTRL,
3857 MSR_TYPE_RW);
3858 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003859 case MSR_IA32_PRED_CMD:
3860 if (!msr_info->host_initiated &&
3861 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3862 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3863 return 1;
3864
3865 if (data & ~PRED_CMD_IBPB)
3866 return 1;
3867
3868 if (!data)
3869 break;
3870
3871 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3872
3873 /*
3874 * For non-nested:
3875 * When it's written (to non-zero) for the first time, pass
3876 * it through.
3877 *
3878 * For nested:
3879 * The handling of the MSR bitmap for L2 guests is done in
3880 * nested_vmx_merge_msr_bitmap. We should not touch the
3881 * vmcs02.msr_bitmap here since it gets completely overwritten
3882 * in the merging.
3883 */
3884 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3885 MSR_TYPE_W);
3886 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003887 case MSR_IA32_ARCH_CAPABILITIES:
3888 if (!msr_info->host_initiated)
3889 return 1;
3890 vmx->arch_capabilities = data;
3891 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003892 case MSR_IA32_CR_PAT:
3893 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003894 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3895 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003896 vmcs_write64(GUEST_IA32_PAT, data);
3897 vcpu->arch.pat = data;
3898 break;
3899 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003900 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003901 break;
Will Auldba904632012-11-29 12:42:50 -08003902 case MSR_IA32_TSC_ADJUST:
3903 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003904 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003905 case MSR_IA32_MCG_EXT_CTL:
3906 if ((!msr_info->host_initiated &&
3907 !(to_vmx(vcpu)->msr_ia32_feature_control &
3908 FEATURE_CONTROL_LMCE)) ||
3909 (data & ~MCG_EXT_CTL_LMCE_EN))
3910 return 1;
3911 vcpu->arch.mcg_ext_ctl = data;
3912 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003913 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003914 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003915 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003916 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3917 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003918 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003919 if (msr_info->host_initiated && data == 0)
3920 vmx_leave_nested(vcpu);
3921 break;
3922 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003923 if (!msr_info->host_initiated)
3924 return 1; /* they are read-only */
3925 if (!nested_vmx_allowed(vcpu))
3926 return 1;
3927 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003928 case MSR_IA32_XSS:
3929 if (!vmx_xsaves_supported())
3930 return 1;
3931 /*
3932 * The only supported bit as of Skylake is bit 8, but
3933 * it is not supported on KVM.
3934 */
3935 if (data != 0)
3936 return 1;
3937 vcpu->arch.ia32_xss = data;
3938 if (vcpu->arch.ia32_xss != host_xss)
3939 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3940 vcpu->arch.ia32_xss, host_xss);
3941 else
3942 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3943 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003944 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003945 if (!msr_info->host_initiated &&
3946 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003947 return 1;
3948 /* Check reserved bit, higher 32 bits should be zero */
3949 if ((data >> 32) != 0)
3950 return 1;
3951 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003953 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003954 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003955 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003956 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003957 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3958 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003959 ret = kvm_set_shared_msr(msr->index, msr->data,
3960 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003961 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003962 if (ret)
3963 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003964 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003965 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003966 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003967 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003968 }
3969
Eddie Dong2cc51562007-05-21 07:28:09 +03003970 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003971}
3972
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003973static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003974{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003975 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3976 switch (reg) {
3977 case VCPU_REGS_RSP:
3978 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3979 break;
3980 case VCPU_REGS_RIP:
3981 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3982 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003983 case VCPU_EXREG_PDPTR:
3984 if (enable_ept)
3985 ept_save_pdptrs(vcpu);
3986 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003987 default:
3988 break;
3989 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003990}
3991
Avi Kivity6aa8b732006-12-10 02:21:36 -08003992static __init int cpu_has_kvm_support(void)
3993{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003994 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995}
3996
3997static __init int vmx_disabled_by_bios(void)
3998{
3999 u64 msr;
4000
4001 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004002 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004003 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004004 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4005 && tboot_enabled())
4006 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004007 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004008 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004009 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004010 && !tboot_enabled()) {
4011 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004012 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004013 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004014 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004015 /* launched w/o TXT and VMX disabled */
4016 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4017 && !tboot_enabled())
4018 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004019 }
4020
4021 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004022}
4023
Dongxiao Xu7725b892010-05-11 18:29:38 +08004024static void kvm_cpu_vmxon(u64 addr)
4025{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004026 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004027 intel_pt_handle_vmx(1);
4028
Dongxiao Xu7725b892010-05-11 18:29:38 +08004029 asm volatile (ASM_VMX_VMXON_RAX
4030 : : "a"(&addr), "m"(addr)
4031 : "memory", "cc");
4032}
4033
Radim Krčmář13a34e02014-08-28 15:13:03 +02004034static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004035{
4036 int cpu = raw_smp_processor_id();
4037 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004038 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004039
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004040 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004041 return -EBUSY;
4042
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004043 /*
4044 * This can happen if we hot-added a CPU but failed to allocate
4045 * VP assist page for it.
4046 */
4047 if (static_branch_unlikely(&enable_evmcs) &&
4048 !hv_get_vp_assist_page(cpu))
4049 return -EFAULT;
4050
Nadav Har'Eld462b812011-05-24 15:26:10 +03004051 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004052 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4053 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004054
4055 /*
4056 * Now we can enable the vmclear operation in kdump
4057 * since the loaded_vmcss_on_cpu list on this cpu
4058 * has been initialized.
4059 *
4060 * Though the cpu is not in VMX operation now, there
4061 * is no problem to enable the vmclear operation
4062 * for the loaded_vmcss_on_cpu list is empty!
4063 */
4064 crash_enable_local_vmclear(cpu);
4065
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004067
4068 test_bits = FEATURE_CONTROL_LOCKED;
4069 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4070 if (tboot_enabled())
4071 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4072
4073 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004075 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4076 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004077 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004078 if (enable_ept)
4079 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004080
4081 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004082}
4083
Nadav Har'Eld462b812011-05-24 15:26:10 +03004084static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004085{
4086 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004087 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004088
Nadav Har'Eld462b812011-05-24 15:26:10 +03004089 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4090 loaded_vmcss_on_cpu_link)
4091 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004092}
4093
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004094
4095/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4096 * tricks.
4097 */
4098static void kvm_cpu_vmxoff(void)
4099{
4100 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004101
4102 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004103 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004104}
4105
Radim Krčmář13a34e02014-08-28 15:13:03 +02004106static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004107{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004108 vmclear_local_loaded_vmcss();
4109 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004110}
4111
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004112static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004113 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114{
4115 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004116 u32 ctl = ctl_min | ctl_opt;
4117
4118 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4119
4120 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4121 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4122
4123 /* Ensure minimum (required) set of control bits are supported. */
4124 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004125 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004126
4127 *result = ctl;
4128 return 0;
4129}
4130
Avi Kivity110312c2010-12-21 12:54:20 +02004131static __init bool allow_1_setting(u32 msr, u32 ctl)
4132{
4133 u32 vmx_msr_low, vmx_msr_high;
4134
4135 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4136 return vmx_msr_high & ctl;
4137}
4138
Yang, Sheng002c7f72007-07-31 14:23:01 +03004139static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004140{
4141 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004142 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004143 u32 _pin_based_exec_control = 0;
4144 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004145 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004146 u32 _vmexit_control = 0;
4147 u32 _vmentry_control = 0;
4148
Paolo Bonzini13893092018-02-26 13:40:09 +01004149 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304150 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004151#ifdef CONFIG_X86_64
4152 CPU_BASED_CR8_LOAD_EXITING |
4153 CPU_BASED_CR8_STORE_EXITING |
4154#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004155 CPU_BASED_CR3_LOAD_EXITING |
4156 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004157 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004158 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004159 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004160 CPU_BASED_MWAIT_EXITING |
4161 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004162 CPU_BASED_INVLPG_EXITING |
4163 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004164
Sheng Yangf78e0e22007-10-29 09:40:42 +08004165 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004166 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004167 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004168 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4169 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004170 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004171#ifdef CONFIG_X86_64
4172 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4173 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4174 ~CPU_BASED_CR8_STORE_EXITING;
4175#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004176 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004177 min2 = 0;
4178 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004179 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004180 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004181 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004182 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004183 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004184 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004185 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004186 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004187 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004188 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004189 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004190 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004191 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004192 SECONDARY_EXEC_RDSEED_EXITING |
4193 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004194 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004195 SECONDARY_EXEC_TSC_SCALING |
4196 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004197 if (adjust_vmx_controls(min2, opt2,
4198 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004199 &_cpu_based_2nd_exec_control) < 0)
4200 return -EIO;
4201 }
4202#ifndef CONFIG_X86_64
4203 if (!(_cpu_based_2nd_exec_control &
4204 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4205 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4206#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004207
4208 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4209 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004210 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004211 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4212 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004213
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004214 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4215 &vmx_capability.ept, &vmx_capability.vpid);
4216
Sheng Yangd56f5462008-04-25 10:13:16 +08004217 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004218 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4219 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004220 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4221 CPU_BASED_CR3_STORE_EXITING |
4222 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004223 } else if (vmx_capability.ept) {
4224 vmx_capability.ept = 0;
4225 pr_warn_once("EPT CAP should not exist if not support "
4226 "1-setting enable EPT VM-execution control\n");
4227 }
4228 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4229 vmx_capability.vpid) {
4230 vmx_capability.vpid = 0;
4231 pr_warn_once("VPID CAP should not exist if not support "
4232 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004233 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004234
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004235 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004236#ifdef CONFIG_X86_64
4237 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4238#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004239 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004240 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004241 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4242 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004243 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004244
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004245 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4246 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4247 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004248 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4249 &_pin_based_exec_control) < 0)
4250 return -EIO;
4251
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004252 if (cpu_has_broken_vmx_preemption_timer())
4253 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004254 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004255 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004256 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4257
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004258 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004259 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004260 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4261 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004262 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004264 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004265
4266 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4267 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004268 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004269
4270#ifdef CONFIG_X86_64
4271 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4272 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004273 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004274#endif
4275
4276 /* Require Write-Back (WB) memory type for VMCS accesses. */
4277 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004278 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004279
Yang, Sheng002c7f72007-07-31 14:23:01 +03004280 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004281 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004282 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004283
4284 /* KVM supports Enlightened VMCS v1 only */
4285 if (static_branch_unlikely(&enable_evmcs))
4286 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4287 else
4288 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004289
Yang, Sheng002c7f72007-07-31 14:23:01 +03004290 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4291 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004292 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004293 vmcs_conf->vmexit_ctrl = _vmexit_control;
4294 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004295
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004296 if (static_branch_unlikely(&enable_evmcs))
4297 evmcs_sanitize_exec_ctrls(vmcs_conf);
4298
Avi Kivity110312c2010-12-21 12:54:20 +02004299 cpu_has_load_ia32_efer =
4300 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4301 VM_ENTRY_LOAD_IA32_EFER)
4302 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4303 VM_EXIT_LOAD_IA32_EFER);
4304
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004305 cpu_has_load_perf_global_ctrl =
4306 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4307 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4308 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4309 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4310
4311 /*
4312 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004313 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004314 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4315 *
4316 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4317 *
4318 * AAK155 (model 26)
4319 * AAP115 (model 30)
4320 * AAT100 (model 37)
4321 * BC86,AAY89,BD102 (model 44)
4322 * BA97 (model 46)
4323 *
4324 */
4325 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4326 switch (boot_cpu_data.x86_model) {
4327 case 26:
4328 case 30:
4329 case 37:
4330 case 44:
4331 case 46:
4332 cpu_has_load_perf_global_ctrl = false;
4333 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4334 "does not work properly. Using workaround\n");
4335 break;
4336 default:
4337 break;
4338 }
4339 }
4340
Borislav Petkov782511b2016-04-04 22:25:03 +02004341 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004342 rdmsrl(MSR_IA32_XSS, host_xss);
4343
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004344 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004345}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004346
4347static struct vmcs *alloc_vmcs_cpu(int cpu)
4348{
4349 int node = cpu_to_node(cpu);
4350 struct page *pages;
4351 struct vmcs *vmcs;
4352
Vlastimil Babka96db8002015-09-08 15:03:50 -07004353 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354 if (!pages)
4355 return NULL;
4356 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004357 memset(vmcs, 0, vmcs_config.size);
4358 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359 return vmcs;
4360}
4361
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362static void free_vmcs(struct vmcs *vmcs)
4363{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004364 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365}
4366
Nadav Har'Eld462b812011-05-24 15:26:10 +03004367/*
4368 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4369 */
4370static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4371{
4372 if (!loaded_vmcs->vmcs)
4373 return;
4374 loaded_vmcs_clear(loaded_vmcs);
4375 free_vmcs(loaded_vmcs->vmcs);
4376 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004377 if (loaded_vmcs->msr_bitmap)
4378 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004379 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004380}
4381
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004382static struct vmcs *alloc_vmcs(void)
4383{
4384 return alloc_vmcs_cpu(raw_smp_processor_id());
4385}
4386
4387static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4388{
4389 loaded_vmcs->vmcs = alloc_vmcs();
4390 if (!loaded_vmcs->vmcs)
4391 return -ENOMEM;
4392
4393 loaded_vmcs->shadow_vmcs = NULL;
4394 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004395
4396 if (cpu_has_vmx_msr_bitmap()) {
4397 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4398 if (!loaded_vmcs->msr_bitmap)
4399 goto out_vmcs;
4400 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004401
4402 if (static_branch_unlikely(&enable_evmcs) &&
4403 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4404 struct hv_enlightened_vmcs *evmcs =
4405 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4406
4407 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4408 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004409 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004410 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004411
4412out_vmcs:
4413 free_loaded_vmcs(loaded_vmcs);
4414 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004415}
4416
Sam Ravnborg39959582007-06-01 00:47:13 -07004417static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004418{
4419 int cpu;
4420
Zachary Amsden3230bb42009-09-29 11:38:37 -10004421 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004422 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004423 per_cpu(vmxarea, cpu) = NULL;
4424 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425}
4426
Jim Mattsond37f4262017-12-22 12:12:16 -08004427enum vmcs_field_width {
4428 VMCS_FIELD_WIDTH_U16 = 0,
4429 VMCS_FIELD_WIDTH_U64 = 1,
4430 VMCS_FIELD_WIDTH_U32 = 2,
4431 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004432};
4433
Jim Mattsond37f4262017-12-22 12:12:16 -08004434static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004435{
4436 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004437 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004438 return (field >> 13) & 0x3 ;
4439}
4440
4441static inline int vmcs_field_readonly(unsigned long field)
4442{
4443 return (((field >> 10) & 0x3) == 1);
4444}
4445
Bandan Dasfe2b2012014-04-21 15:20:14 -04004446static void init_vmcs_shadow_fields(void)
4447{
4448 int i, j;
4449
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004450 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4451 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004452 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004453 (i + 1 == max_shadow_read_only_fields ||
4454 shadow_read_only_fields[i + 1] != field + 1))
4455 pr_err("Missing field from shadow_read_only_field %x\n",
4456 field + 1);
4457
4458 clear_bit(field, vmx_vmread_bitmap);
4459#ifdef CONFIG_X86_64
4460 if (field & 1)
4461 continue;
4462#endif
4463 if (j < i)
4464 shadow_read_only_fields[j] = field;
4465 j++;
4466 }
4467 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004468
4469 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004470 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004471 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004472 (i + 1 == max_shadow_read_write_fields ||
4473 shadow_read_write_fields[i + 1] != field + 1))
4474 pr_err("Missing field from shadow_read_write_field %x\n",
4475 field + 1);
4476
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004477 /*
4478 * PML and the preemption timer can be emulated, but the
4479 * processor cannot vmwrite to fields that don't exist
4480 * on bare metal.
4481 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004482 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004483 case GUEST_PML_INDEX:
4484 if (!cpu_has_vmx_pml())
4485 continue;
4486 break;
4487 case VMX_PREEMPTION_TIMER_VALUE:
4488 if (!cpu_has_vmx_preemption_timer())
4489 continue;
4490 break;
4491 case GUEST_INTR_STATUS:
4492 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004493 continue;
4494 break;
4495 default:
4496 break;
4497 }
4498
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004499 clear_bit(field, vmx_vmwrite_bitmap);
4500 clear_bit(field, vmx_vmread_bitmap);
4501#ifdef CONFIG_X86_64
4502 if (field & 1)
4503 continue;
4504#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004505 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004506 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004507 j++;
4508 }
4509 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004510}
4511
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512static __init int alloc_kvm_area(void)
4513{
4514 int cpu;
4515
Zachary Amsden3230bb42009-09-29 11:38:37 -10004516 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004517 struct vmcs *vmcs;
4518
4519 vmcs = alloc_vmcs_cpu(cpu);
4520 if (!vmcs) {
4521 free_kvm_area();
4522 return -ENOMEM;
4523 }
4524
4525 per_cpu(vmxarea, cpu) = vmcs;
4526 }
4527 return 0;
4528}
4529
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004530static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004531 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004532{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004533 if (!emulate_invalid_guest_state) {
4534 /*
4535 * CS and SS RPL should be equal during guest entry according
4536 * to VMX spec, but in reality it is not always so. Since vcpu
4537 * is in the middle of the transition from real mode to
4538 * protected mode it is safe to assume that RPL 0 is a good
4539 * default value.
4540 */
4541 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004542 save->selector &= ~SEGMENT_RPL_MASK;
4543 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004544 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004545 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004546 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004547}
4548
4549static void enter_pmode(struct kvm_vcpu *vcpu)
4550{
4551 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004552 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004553
Gleb Natapovd99e4152012-12-20 16:57:45 +02004554 /*
4555 * Update real mode segment cache. It may be not up-to-date if sement
4556 * register was written while vcpu was in a guest mode.
4557 */
4558 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4559 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4560 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4561 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4562 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4563 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4564
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004565 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566
Avi Kivity2fb92db2011-04-27 19:42:18 +03004567 vmx_segment_cache_clear(vmx);
4568
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004569 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004570
4571 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004572 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4573 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004574 vmcs_writel(GUEST_RFLAGS, flags);
4575
Rusty Russell66aee912007-07-17 23:34:16 +10004576 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4577 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004578
4579 update_exception_bitmap(vcpu);
4580
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004581 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4582 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4583 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4584 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4585 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4586 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004587}
4588
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004589static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004590{
Mathias Krause772e0312012-08-30 01:30:19 +02004591 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004592 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004593
Gleb Natapovd99e4152012-12-20 16:57:45 +02004594 var.dpl = 0x3;
4595 if (seg == VCPU_SREG_CS)
4596 var.type = 0x3;
4597
4598 if (!emulate_invalid_guest_state) {
4599 var.selector = var.base >> 4;
4600 var.base = var.base & 0xffff0;
4601 var.limit = 0xffff;
4602 var.g = 0;
4603 var.db = 0;
4604 var.present = 1;
4605 var.s = 1;
4606 var.l = 0;
4607 var.unusable = 0;
4608 var.type = 0x3;
4609 var.avl = 0;
4610 if (save->base & 0xf)
4611 printk_once(KERN_WARNING "kvm: segment base is not "
4612 "paragraph aligned when entering "
4613 "protected mode (seg=%d)", seg);
4614 }
4615
4616 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004617 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004618 vmcs_write32(sf->limit, var.limit);
4619 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004620}
4621
4622static void enter_rmode(struct kvm_vcpu *vcpu)
4623{
4624 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004625 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004626 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004627
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004628 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4629 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4630 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4631 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4632 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004633 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4634 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004635
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004636 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004637
Gleb Natapov776e58e2011-03-13 12:34:27 +02004638 /*
4639 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004640 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004641 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004642 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004643 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4644 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004645
Avi Kivity2fb92db2011-04-27 19:42:18 +03004646 vmx_segment_cache_clear(vmx);
4647
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004648 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004649 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004650 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4651
4652 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004653 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004654
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004655 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004656
4657 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004658 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004659 update_exception_bitmap(vcpu);
4660
Gleb Natapovd99e4152012-12-20 16:57:45 +02004661 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4662 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4663 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4664 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4665 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4666 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004667
Eddie Dong8668a3c2007-10-10 14:26:45 +08004668 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004669}
4670
Amit Shah401d10d2009-02-20 22:53:37 +05304671static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4672{
4673 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004674 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4675
4676 if (!msr)
4677 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304678
Avi Kivity44ea2b12009-09-06 15:55:37 +03004679 /*
4680 * Force kernel_gs_base reloading before EFER changes, as control
4681 * of this msr depends on is_long_mode().
4682 */
4683 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004684 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304685 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004686 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304687 msr->data = efer;
4688 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004689 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304690
4691 msr->data = efer & ~EFER_LME;
4692 }
4693 setup_msrs(vmx);
4694}
4695
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004696#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004697
4698static void enter_lmode(struct kvm_vcpu *vcpu)
4699{
4700 u32 guest_tr_ar;
4701
Avi Kivity2fb92db2011-04-27 19:42:18 +03004702 vmx_segment_cache_clear(to_vmx(vcpu));
4703
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004705 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004706 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4707 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004709 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4710 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004711 }
Avi Kivityda38f432010-07-06 11:30:49 +03004712 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004713}
4714
4715static void exit_lmode(struct kvm_vcpu *vcpu)
4716{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004717 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004718 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004719}
4720
4721#endif
4722
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004723static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4724 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004725{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004726 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004727 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4728 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004729 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004730 } else {
4731 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004732 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004733}
4734
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004735static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004736{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004737 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004738}
4739
Avi Kivitye8467fd2009-12-29 18:43:06 +02004740static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4741{
4742 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4743
4744 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4745 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4746}
4747
Avi Kivityaff48ba2010-12-05 18:56:11 +02004748static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4749{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004750 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004751 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4752 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4753}
4754
Anthony Liguori25c4c272007-04-27 09:29:21 +03004755static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004756{
Avi Kivityfc78f512009-12-07 12:16:48 +02004757 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4758
4759 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4760 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004761}
4762
Sheng Yang14394422008-04-28 12:24:45 +08004763static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4764{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004765 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4766
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004767 if (!test_bit(VCPU_EXREG_PDPTR,
4768 (unsigned long *)&vcpu->arch.regs_dirty))
4769 return;
4770
Sheng Yang14394422008-04-28 12:24:45 +08004771 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004772 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4773 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4774 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4775 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004776 }
4777}
4778
Avi Kivity8f5d5492009-05-31 18:41:29 +03004779static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4780{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004781 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4782
Avi Kivity8f5d5492009-05-31 18:41:29 +03004783 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004784 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4785 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4786 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4787 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004788 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004789
4790 __set_bit(VCPU_EXREG_PDPTR,
4791 (unsigned long *)&vcpu->arch.regs_avail);
4792 __set_bit(VCPU_EXREG_PDPTR,
4793 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004794}
4795
David Matlack38991522016-11-29 18:14:08 -08004796static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4797{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004798 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4799 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004800 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4801
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004802 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004803 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4804 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4805 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4806
4807 return fixed_bits_valid(val, fixed0, fixed1);
4808}
4809
4810static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4811{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004812 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4813 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004814
4815 return fixed_bits_valid(val, fixed0, fixed1);
4816}
4817
4818static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4819{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004820 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4821 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004822
4823 return fixed_bits_valid(val, fixed0, fixed1);
4824}
4825
4826/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4827#define nested_guest_cr4_valid nested_cr4_valid
4828#define nested_host_cr4_valid nested_cr4_valid
4829
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004830static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004831
4832static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4833 unsigned long cr0,
4834 struct kvm_vcpu *vcpu)
4835{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004836 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4837 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004838 if (!(cr0 & X86_CR0_PG)) {
4839 /* From paging/starting to nonpaging */
4840 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004841 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004842 (CPU_BASED_CR3_LOAD_EXITING |
4843 CPU_BASED_CR3_STORE_EXITING));
4844 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004845 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004846 } else if (!is_paging(vcpu)) {
4847 /* From nonpaging to paging */
4848 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004849 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004850 ~(CPU_BASED_CR3_LOAD_EXITING |
4851 CPU_BASED_CR3_STORE_EXITING));
4852 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004853 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004854 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004855
4856 if (!(cr0 & X86_CR0_WP))
4857 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004858}
4859
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4861{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004862 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004863 unsigned long hw_cr0;
4864
Gleb Natapov50378782013-02-04 16:00:28 +02004865 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004866 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004867 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004868 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004869 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004870
Gleb Natapov218e7632013-01-21 15:36:45 +02004871 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4872 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004873
Gleb Natapov218e7632013-01-21 15:36:45 +02004874 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4875 enter_rmode(vcpu);
4876 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004878#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004879 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004880 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004881 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004882 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883 exit_lmode(vcpu);
4884 }
4885#endif
4886
Sean Christophersonb4d18512018-03-05 12:04:40 -08004887 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004888 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4889
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004891 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004892 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004893
4894 /* depends on vcpu->arch.cr0 to be set to a new value */
4895 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896}
4897
Yu Zhang855feb62017-08-24 20:27:55 +08004898static int get_ept_level(struct kvm_vcpu *vcpu)
4899{
4900 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4901 return 5;
4902 return 4;
4903}
4904
Peter Feiner995f00a2017-06-30 17:26:32 -07004905static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004906{
Yu Zhang855feb62017-08-24 20:27:55 +08004907 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004908
Yu Zhang855feb62017-08-24 20:27:55 +08004909 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004910
Peter Feiner995f00a2017-06-30 17:26:32 -07004911 if (enable_ept_ad_bits &&
4912 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004913 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004914 eptp |= (root_hpa & PAGE_MASK);
4915
4916 return eptp;
4917}
4918
Avi Kivity6aa8b732006-12-10 02:21:36 -08004919static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4920{
Sheng Yang14394422008-04-28 12:24:45 +08004921 unsigned long guest_cr3;
4922 u64 eptp;
4923
4924 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004925 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004926 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004927 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004928 if (enable_unrestricted_guest || is_paging(vcpu) ||
4929 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004930 guest_cr3 = kvm_read_cr3(vcpu);
4931 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004932 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004933 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004934 }
4935
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004936 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004937 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004938}
4939
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004940static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004941{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004942 /*
4943 * Pass through host's Machine Check Enable value to hw_cr4, which
4944 * is in force while we are in guest mode. Do not let guests control
4945 * this bit, even if host CR4.MCE == 0.
4946 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004947 unsigned long hw_cr4;
4948
4949 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4950 if (enable_unrestricted_guest)
4951 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4952 else if (to_vmx(vcpu)->rmode.vm86_active)
4953 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4954 else
4955 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004956
Sean Christopherson64f7a112018-04-30 10:01:06 -07004957 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
4958 if (cr4 & X86_CR4_UMIP) {
4959 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02004960 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07004961 hw_cr4 &= ~X86_CR4_UMIP;
4962 } else if (!is_guest_mode(vcpu) ||
4963 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
4964 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4965 SECONDARY_EXEC_DESC);
4966 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02004967
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004968 if (cr4 & X86_CR4_VMXE) {
4969 /*
4970 * To use VMXON (and later other VMX instructions), a guest
4971 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4972 * So basically the check on whether to allow nested VMX
4973 * is here.
4974 */
4975 if (!nested_vmx_allowed(vcpu))
4976 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004977 }
David Matlack38991522016-11-29 18:14:08 -08004978
4979 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004980 return 1;
4981
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004982 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004983
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004984 if (!enable_unrestricted_guest) {
4985 if (enable_ept) {
4986 if (!is_paging(vcpu)) {
4987 hw_cr4 &= ~X86_CR4_PAE;
4988 hw_cr4 |= X86_CR4_PSE;
4989 } else if (!(cr4 & X86_CR4_PAE)) {
4990 hw_cr4 &= ~X86_CR4_PAE;
4991 }
4992 }
4993
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004994 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004995 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4996 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4997 * to be manually disabled when guest switches to non-paging
4998 * mode.
4999 *
5000 * If !enable_unrestricted_guest, the CPU is always running
5001 * with CR0.PG=1 and CR4 needs to be modified.
5002 * If enable_unrestricted_guest, the CPU automatically
5003 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005004 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005005 if (!is_paging(vcpu))
5006 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5007 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005008
Sheng Yang14394422008-04-28 12:24:45 +08005009 vmcs_writel(CR4_READ_SHADOW, cr4);
5010 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005011 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005012}
5013
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014static void vmx_get_segment(struct kvm_vcpu *vcpu,
5015 struct kvm_segment *var, int seg)
5016{
Avi Kivitya9179492011-01-03 14:28:52 +02005017 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018 u32 ar;
5019
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005020 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005021 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005022 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005023 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005024 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005025 var->base = vmx_read_guest_seg_base(vmx, seg);
5026 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5027 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005028 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005029 var->base = vmx_read_guest_seg_base(vmx, seg);
5030 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5031 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5032 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005033 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034 var->type = ar & 15;
5035 var->s = (ar >> 4) & 1;
5036 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005037 /*
5038 * Some userspaces do not preserve unusable property. Since usable
5039 * segment has to be present according to VMX spec we can use present
5040 * property to amend userspace bug by making unusable segment always
5041 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5042 * segment as unusable.
5043 */
5044 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005045 var->avl = (ar >> 12) & 1;
5046 var->l = (ar >> 13) & 1;
5047 var->db = (ar >> 14) & 1;
5048 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005049}
5050
Avi Kivitya9179492011-01-03 14:28:52 +02005051static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5052{
Avi Kivitya9179492011-01-03 14:28:52 +02005053 struct kvm_segment s;
5054
5055 if (to_vmx(vcpu)->rmode.vm86_active) {
5056 vmx_get_segment(vcpu, &s, seg);
5057 return s.base;
5058 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005059 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005060}
5061
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005062static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005063{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005064 struct vcpu_vmx *vmx = to_vmx(vcpu);
5065
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005066 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005067 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005068 else {
5069 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005070 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005071 }
Avi Kivity69c73022011-03-07 15:26:44 +02005072}
5073
Avi Kivity653e3102007-05-07 10:55:37 +03005074static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005076 u32 ar;
5077
Avi Kivityf0495f92012-06-07 17:06:10 +03005078 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079 ar = 1 << 16;
5080 else {
5081 ar = var->type & 15;
5082 ar |= (var->s & 1) << 4;
5083 ar |= (var->dpl & 3) << 5;
5084 ar |= (var->present & 1) << 7;
5085 ar |= (var->avl & 1) << 12;
5086 ar |= (var->l & 1) << 13;
5087 ar |= (var->db & 1) << 14;
5088 ar |= (var->g & 1) << 15;
5089 }
Avi Kivity653e3102007-05-07 10:55:37 +03005090
5091 return ar;
5092}
5093
5094static void vmx_set_segment(struct kvm_vcpu *vcpu,
5095 struct kvm_segment *var, int seg)
5096{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005097 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005098 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005099
Avi Kivity2fb92db2011-04-27 19:42:18 +03005100 vmx_segment_cache_clear(vmx);
5101
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005102 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5103 vmx->rmode.segs[seg] = *var;
5104 if (seg == VCPU_SREG_TR)
5105 vmcs_write16(sf->selector, var->selector);
5106 else if (var->s)
5107 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005108 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005109 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005110
Avi Kivity653e3102007-05-07 10:55:37 +03005111 vmcs_writel(sf->base, var->base);
5112 vmcs_write32(sf->limit, var->limit);
5113 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005114
5115 /*
5116 * Fix the "Accessed" bit in AR field of segment registers for older
5117 * qemu binaries.
5118 * IA32 arch specifies that at the time of processor reset the
5119 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005120 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005121 * state vmexit when "unrestricted guest" mode is turned on.
5122 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5123 * tree. Newer qemu binaries with that qemu fix would not need this
5124 * kvm hack.
5125 */
5126 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005127 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005128
Gleb Natapovf924d662012-12-12 19:10:55 +02005129 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005130
5131out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005132 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005133}
5134
Avi Kivity6aa8b732006-12-10 02:21:36 -08005135static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5136{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005137 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005138
5139 *db = (ar >> 14) & 1;
5140 *l = (ar >> 13) & 1;
5141}
5142
Gleb Natapov89a27f42010-02-16 10:51:48 +02005143static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005144{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005145 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5146 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005147}
5148
Gleb Natapov89a27f42010-02-16 10:51:48 +02005149static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005150{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005151 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5152 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005153}
5154
Gleb Natapov89a27f42010-02-16 10:51:48 +02005155static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005156{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005157 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5158 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005159}
5160
Gleb Natapov89a27f42010-02-16 10:51:48 +02005161static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005162{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005163 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5164 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005165}
5166
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005167static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5168{
5169 struct kvm_segment var;
5170 u32 ar;
5171
5172 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005173 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005174 if (seg == VCPU_SREG_CS)
5175 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005176 ar = vmx_segment_access_rights(&var);
5177
5178 if (var.base != (var.selector << 4))
5179 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005180 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005181 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005182 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005183 return false;
5184
5185 return true;
5186}
5187
5188static bool code_segment_valid(struct kvm_vcpu *vcpu)
5189{
5190 struct kvm_segment cs;
5191 unsigned int cs_rpl;
5192
5193 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005194 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005195
Avi Kivity1872a3f2009-01-04 23:26:52 +02005196 if (cs.unusable)
5197 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005198 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005199 return false;
5200 if (!cs.s)
5201 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005202 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005203 if (cs.dpl > cs_rpl)
5204 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005205 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005206 if (cs.dpl != cs_rpl)
5207 return false;
5208 }
5209 if (!cs.present)
5210 return false;
5211
5212 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5213 return true;
5214}
5215
5216static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5217{
5218 struct kvm_segment ss;
5219 unsigned int ss_rpl;
5220
5221 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005222 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005223
Avi Kivity1872a3f2009-01-04 23:26:52 +02005224 if (ss.unusable)
5225 return true;
5226 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005227 return false;
5228 if (!ss.s)
5229 return false;
5230 if (ss.dpl != ss_rpl) /* DPL != RPL */
5231 return false;
5232 if (!ss.present)
5233 return false;
5234
5235 return true;
5236}
5237
5238static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5239{
5240 struct kvm_segment var;
5241 unsigned int rpl;
5242
5243 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005244 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005245
Avi Kivity1872a3f2009-01-04 23:26:52 +02005246 if (var.unusable)
5247 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005248 if (!var.s)
5249 return false;
5250 if (!var.present)
5251 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005252 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005253 if (var.dpl < rpl) /* DPL < RPL */
5254 return false;
5255 }
5256
5257 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5258 * rights flags
5259 */
5260 return true;
5261}
5262
5263static bool tr_valid(struct kvm_vcpu *vcpu)
5264{
5265 struct kvm_segment tr;
5266
5267 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5268
Avi Kivity1872a3f2009-01-04 23:26:52 +02005269 if (tr.unusable)
5270 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005271 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005272 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005273 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005274 return false;
5275 if (!tr.present)
5276 return false;
5277
5278 return true;
5279}
5280
5281static bool ldtr_valid(struct kvm_vcpu *vcpu)
5282{
5283 struct kvm_segment ldtr;
5284
5285 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5286
Avi Kivity1872a3f2009-01-04 23:26:52 +02005287 if (ldtr.unusable)
5288 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005289 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005290 return false;
5291 if (ldtr.type != 2)
5292 return false;
5293 if (!ldtr.present)
5294 return false;
5295
5296 return true;
5297}
5298
5299static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5300{
5301 struct kvm_segment cs, ss;
5302
5303 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5304 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5305
Nadav Amitb32a9912015-03-29 16:33:04 +03005306 return ((cs.selector & SEGMENT_RPL_MASK) ==
5307 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005308}
5309
5310/*
5311 * Check if guest state is valid. Returns true if valid, false if
5312 * not.
5313 * We assume that registers are always usable
5314 */
5315static bool guest_state_valid(struct kvm_vcpu *vcpu)
5316{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005317 if (enable_unrestricted_guest)
5318 return true;
5319
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005320 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005321 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005322 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5323 return false;
5324 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5325 return false;
5326 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5327 return false;
5328 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5329 return false;
5330 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5331 return false;
5332 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5333 return false;
5334 } else {
5335 /* protected mode guest state checks */
5336 if (!cs_ss_rpl_check(vcpu))
5337 return false;
5338 if (!code_segment_valid(vcpu))
5339 return false;
5340 if (!stack_segment_valid(vcpu))
5341 return false;
5342 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5343 return false;
5344 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5345 return false;
5346 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5347 return false;
5348 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5349 return false;
5350 if (!tr_valid(vcpu))
5351 return false;
5352 if (!ldtr_valid(vcpu))
5353 return false;
5354 }
5355 /* TODO:
5356 * - Add checks on RIP
5357 * - Add checks on RFLAGS
5358 */
5359
5360 return true;
5361}
5362
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005363static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5364{
5365 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5366}
5367
Mike Dayd77c26f2007-10-08 09:02:08 -04005368static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005369{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005370 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005371 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005372 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005373
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005374 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005375 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005376 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5377 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005378 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005379 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005380 r = kvm_write_guest_page(kvm, fn++, &data,
5381 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005382 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005383 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005384 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5385 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005386 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005387 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5388 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005389 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005390 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005391 r = kvm_write_guest_page(kvm, fn, &data,
5392 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5393 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005394out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005395 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005396 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005397}
5398
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005399static int init_rmode_identity_map(struct kvm *kvm)
5400{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005401 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005402 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005403 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005404 u32 tmp;
5405
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005406 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005407 mutex_lock(&kvm->slots_lock);
5408
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005409 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005410 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005411
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005412 if (!kvm_vmx->ept_identity_map_addr)
5413 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5414 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005415
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005416 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005417 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005418 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005419 goto out2;
5420
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005421 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005422 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5423 if (r < 0)
5424 goto out;
5425 /* Set up identity-mapping pagetable for EPT in real mode */
5426 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5427 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5428 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5429 r = kvm_write_guest_page(kvm, identity_map_pfn,
5430 &tmp, i * sizeof(tmp), sizeof(tmp));
5431 if (r < 0)
5432 goto out;
5433 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005434 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005435
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005436out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005437 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005438
5439out2:
5440 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005441 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005442}
5443
Avi Kivity6aa8b732006-12-10 02:21:36 -08005444static void seg_setup(int seg)
5445{
Mathias Krause772e0312012-08-30 01:30:19 +02005446 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005447 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005448
5449 vmcs_write16(sf->selector, 0);
5450 vmcs_writel(sf->base, 0);
5451 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005452 ar = 0x93;
5453 if (seg == VCPU_SREG_CS)
5454 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005455
5456 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457}
5458
Sheng Yangf78e0e22007-10-29 09:40:42 +08005459static int alloc_apic_access_page(struct kvm *kvm)
5460{
Xiao Guangrong44841412012-09-07 14:14:20 +08005461 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005462 int r = 0;
5463
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005464 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005465 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005466 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005467 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5468 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005469 if (r)
5470 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005471
Tang Chen73a6d942014-09-11 13:38:00 +08005472 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005473 if (is_error_page(page)) {
5474 r = -EFAULT;
5475 goto out;
5476 }
5477
Tang Chenc24ae0d2014-09-24 15:57:58 +08005478 /*
5479 * Do not pin the page in memory, so that memory hot-unplug
5480 * is able to migrate it.
5481 */
5482 put_page(page);
5483 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005484out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005485 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005486 return r;
5487}
5488
Wanpeng Li991e7a02015-09-16 17:30:05 +08005489static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005490{
5491 int vpid;
5492
Avi Kivity919818a2009-03-23 18:01:29 +02005493 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005494 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005495 spin_lock(&vmx_vpid_lock);
5496 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005497 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005498 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005499 else
5500 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005501 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005502 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005503}
5504
Wanpeng Li991e7a02015-09-16 17:30:05 +08005505static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005506{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005507 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005508 return;
5509 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005510 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005511 spin_unlock(&vmx_vpid_lock);
5512}
5513
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005514static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5515 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005516{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005517 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005518
5519 if (!cpu_has_vmx_msr_bitmap())
5520 return;
5521
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005522 if (static_branch_unlikely(&enable_evmcs))
5523 evmcs_touch_msr_bitmap();
5524
Sheng Yang25c5f222008-03-28 13:18:56 +08005525 /*
5526 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5527 * have the write-low and read-high bitmap offsets the wrong way round.
5528 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5529 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005530 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005531 if (type & MSR_TYPE_R)
5532 /* read-low */
5533 __clear_bit(msr, msr_bitmap + 0x000 / f);
5534
5535 if (type & MSR_TYPE_W)
5536 /* write-low */
5537 __clear_bit(msr, msr_bitmap + 0x800 / f);
5538
Sheng Yang25c5f222008-03-28 13:18:56 +08005539 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5540 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005541 if (type & MSR_TYPE_R)
5542 /* read-high */
5543 __clear_bit(msr, msr_bitmap + 0x400 / f);
5544
5545 if (type & MSR_TYPE_W)
5546 /* write-high */
5547 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5548
5549 }
5550}
5551
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005552static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5553 u32 msr, int type)
5554{
5555 int f = sizeof(unsigned long);
5556
5557 if (!cpu_has_vmx_msr_bitmap())
5558 return;
5559
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005560 if (static_branch_unlikely(&enable_evmcs))
5561 evmcs_touch_msr_bitmap();
5562
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005563 /*
5564 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5565 * have the write-low and read-high bitmap offsets the wrong way round.
5566 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5567 */
5568 if (msr <= 0x1fff) {
5569 if (type & MSR_TYPE_R)
5570 /* read-low */
5571 __set_bit(msr, msr_bitmap + 0x000 / f);
5572
5573 if (type & MSR_TYPE_W)
5574 /* write-low */
5575 __set_bit(msr, msr_bitmap + 0x800 / f);
5576
5577 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5578 msr &= 0x1fff;
5579 if (type & MSR_TYPE_R)
5580 /* read-high */
5581 __set_bit(msr, msr_bitmap + 0x400 / f);
5582
5583 if (type & MSR_TYPE_W)
5584 /* write-high */
5585 __set_bit(msr, msr_bitmap + 0xc00 / f);
5586
5587 }
5588}
5589
5590static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5591 u32 msr, int type, bool value)
5592{
5593 if (value)
5594 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5595 else
5596 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5597}
5598
Wincy Vanf2b93282015-02-03 23:56:03 +08005599/*
5600 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5601 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5602 */
5603static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5604 unsigned long *msr_bitmap_nested,
5605 u32 msr, int type)
5606{
5607 int f = sizeof(unsigned long);
5608
Wincy Vanf2b93282015-02-03 23:56:03 +08005609 /*
5610 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5611 * have the write-low and read-high bitmap offsets the wrong way round.
5612 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5613 */
5614 if (msr <= 0x1fff) {
5615 if (type & MSR_TYPE_R &&
5616 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5617 /* read-low */
5618 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5619
5620 if (type & MSR_TYPE_W &&
5621 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5622 /* write-low */
5623 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5624
5625 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5626 msr &= 0x1fff;
5627 if (type & MSR_TYPE_R &&
5628 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5629 /* read-high */
5630 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5631
5632 if (type & MSR_TYPE_W &&
5633 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5634 /* write-high */
5635 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5636
5637 }
5638}
5639
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005640static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005641{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005642 u8 mode = 0;
5643
5644 if (cpu_has_secondary_exec_ctrls() &&
5645 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5646 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5647 mode |= MSR_BITMAP_MODE_X2APIC;
5648 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5649 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5650 }
5651
5652 if (is_long_mode(vcpu))
5653 mode |= MSR_BITMAP_MODE_LM;
5654
5655 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005656}
5657
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005658#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5659
5660static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5661 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005662{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005663 int msr;
5664
5665 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5666 unsigned word = msr / BITS_PER_LONG;
5667 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5668 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005669 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005670
5671 if (mode & MSR_BITMAP_MODE_X2APIC) {
5672 /*
5673 * TPR reads and writes can be virtualized even if virtual interrupt
5674 * delivery is not in use.
5675 */
5676 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5677 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5678 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5679 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5680 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5681 }
5682 }
5683}
5684
5685static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5686{
5687 struct vcpu_vmx *vmx = to_vmx(vcpu);
5688 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5689 u8 mode = vmx_msr_bitmap_mode(vcpu);
5690 u8 changed = mode ^ vmx->msr_bitmap_mode;
5691
5692 if (!changed)
5693 return;
5694
5695 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5696 !(mode & MSR_BITMAP_MODE_LM));
5697
5698 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5699 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5700
5701 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005702}
5703
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005704static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005705{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005706 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005707}
5708
David Matlackc9f04402017-08-01 14:00:40 -07005709static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5710{
5711 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5712 gfn_t gfn;
5713
5714 /*
5715 * Don't need to mark the APIC access page dirty; it is never
5716 * written to by the CPU during APIC virtualization.
5717 */
5718
5719 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5720 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5721 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5722 }
5723
5724 if (nested_cpu_has_posted_intr(vmcs12)) {
5725 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5726 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5727 }
5728}
5729
5730
David Hildenbrand6342c502017-01-25 11:58:58 +01005731static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005732{
5733 struct vcpu_vmx *vmx = to_vmx(vcpu);
5734 int max_irr;
5735 void *vapic_page;
5736 u16 status;
5737
David Matlackc9f04402017-08-01 14:00:40 -07005738 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5739 return;
Wincy Van705699a2015-02-03 23:58:17 +08005740
David Matlackc9f04402017-08-01 14:00:40 -07005741 vmx->nested.pi_pending = false;
5742 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5743 return;
Wincy Van705699a2015-02-03 23:58:17 +08005744
David Matlackc9f04402017-08-01 14:00:40 -07005745 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5746 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005747 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005748 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5749 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005750 kunmap(vmx->nested.virtual_apic_page);
5751
5752 status = vmcs_read16(GUEST_INTR_STATUS);
5753 if ((u8)max_irr > ((u8)status & 0xff)) {
5754 status &= ~0xff;
5755 status |= (u8)max_irr;
5756 vmcs_write16(GUEST_INTR_STATUS, status);
5757 }
5758 }
David Matlackc9f04402017-08-01 14:00:40 -07005759
5760 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005761}
5762
Wincy Van06a55242017-04-28 13:13:59 +08005763static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5764 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005765{
5766#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005767 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5768
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005769 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005770 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005771 * The vector of interrupt to be delivered to vcpu had
5772 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005773 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005774 * Following cases will be reached in this block, and
5775 * we always send a notification event in all cases as
5776 * explained below.
5777 *
5778 * Case 1: vcpu keeps in non-root mode. Sending a
5779 * notification event posts the interrupt to vcpu.
5780 *
5781 * Case 2: vcpu exits to root mode and is still
5782 * runnable. PIR will be synced to vIRR before the
5783 * next vcpu entry. Sending a notification event in
5784 * this case has no effect, as vcpu is not in root
5785 * mode.
5786 *
5787 * Case 3: vcpu exits to root mode and is blocked.
5788 * vcpu_block() has already synced PIR to vIRR and
5789 * never blocks vcpu if vIRR is not cleared. Therefore,
5790 * a blocked vcpu here does not wait for any requested
5791 * interrupts in PIR, and sending a notification event
5792 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005793 */
Feng Wu28b835d2015-09-18 22:29:54 +08005794
Wincy Van06a55242017-04-28 13:13:59 +08005795 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005796 return true;
5797 }
5798#endif
5799 return false;
5800}
5801
Wincy Van705699a2015-02-03 23:58:17 +08005802static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5803 int vector)
5804{
5805 struct vcpu_vmx *vmx = to_vmx(vcpu);
5806
5807 if (is_guest_mode(vcpu) &&
5808 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005809 /*
5810 * If a posted intr is not recognized by hardware,
5811 * we will accomplish it in the next vmentry.
5812 */
5813 vmx->nested.pi_pending = true;
5814 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005815 /* the PIR and ON have been set by L1. */
5816 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5817 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005818 return 0;
5819 }
5820 return -1;
5821}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005822/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005823 * Send interrupt to vcpu via posted interrupt way.
5824 * 1. If target vcpu is running(non-root mode), send posted interrupt
5825 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5826 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5827 * interrupt from PIR in next vmentry.
5828 */
5829static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5830{
5831 struct vcpu_vmx *vmx = to_vmx(vcpu);
5832 int r;
5833
Wincy Van705699a2015-02-03 23:58:17 +08005834 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5835 if (!r)
5836 return;
5837
Yang Zhanga20ed542013-04-11 19:25:15 +08005838 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5839 return;
5840
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005841 /* If a previous notification has sent the IPI, nothing to do. */
5842 if (pi_test_and_set_on(&vmx->pi_desc))
5843 return;
5844
Wincy Van06a55242017-04-28 13:13:59 +08005845 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005846 kvm_vcpu_kick(vcpu);
5847}
5848
Avi Kivity6aa8b732006-12-10 02:21:36 -08005849/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005850 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5851 * will not change in the lifetime of the guest.
5852 * Note that host-state that does change is set elsewhere. E.g., host-state
5853 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5854 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005855static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005856{
5857 u32 low32, high32;
5858 unsigned long tmpl;
5859 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005860 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005861
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005862 cr0 = read_cr0();
5863 WARN_ON(cr0 & X86_CR0_TS);
5864 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005865
5866 /*
5867 * Save the most likely value for this task's CR3 in the VMCS.
5868 * We can't use __get_current_cr3_fast() because we're not atomic.
5869 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005870 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005871 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005872 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005873
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005874 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005875 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005876 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005877 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005878
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005879 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005880#ifdef CONFIG_X86_64
5881 /*
5882 * Load null selectors, so we can avoid reloading them in
5883 * __vmx_load_host_state(), in case userspace uses the null selectors
5884 * too (the expected case).
5885 */
5886 vmcs_write16(HOST_DS_SELECTOR, 0);
5887 vmcs_write16(HOST_ES_SELECTOR, 0);
5888#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005889 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5890 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005891#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005892 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5893 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5894
Juergen Gross87930012017-09-04 12:25:27 +02005895 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005896 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005897 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005898
Avi Kivity83287ea422012-09-16 15:10:57 +03005899 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005900
5901 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5902 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5903 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5904 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5905
5906 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5907 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5908 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5909 }
5910}
5911
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005912static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5913{
5914 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5915 if (enable_ept)
5916 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005917 if (is_guest_mode(&vmx->vcpu))
5918 vmx->vcpu.arch.cr4_guest_owned_bits &=
5919 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005920 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5921}
5922
Yang Zhang01e439b2013-04-11 19:25:12 +08005923static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5924{
5925 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5926
Andrey Smetanind62caab2015-11-10 15:36:33 +03005927 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005928 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005929
5930 if (!enable_vnmi)
5931 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5932
Yunhong Jiang64672c92016-06-13 14:19:59 -07005933 /* Enable the preemption timer dynamically */
5934 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005935 return pin_based_exec_ctrl;
5936}
5937
Andrey Smetanind62caab2015-11-10 15:36:33 +03005938static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5939{
5940 struct vcpu_vmx *vmx = to_vmx(vcpu);
5941
5942 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005943 if (cpu_has_secondary_exec_ctrls()) {
5944 if (kvm_vcpu_apicv_active(vcpu))
5945 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5946 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5947 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5948 else
5949 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5950 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5951 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5952 }
5953
5954 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005955 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005956}
5957
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005958static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5959{
5960 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005961
5962 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5963 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5964
Paolo Bonzini35754c92015-07-29 12:05:37 +02005965 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005966 exec_control &= ~CPU_BASED_TPR_SHADOW;
5967#ifdef CONFIG_X86_64
5968 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5969 CPU_BASED_CR8_LOAD_EXITING;
5970#endif
5971 }
5972 if (!enable_ept)
5973 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5974 CPU_BASED_CR3_LOAD_EXITING |
5975 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005976 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5977 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5978 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005979 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5980 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005981 return exec_control;
5982}
5983
Jim Mattson45ec3682017-08-23 16:32:04 -07005984static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005985{
Jim Mattson45ec3682017-08-23 16:32:04 -07005986 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005987 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005988}
5989
Jim Mattson75f4fc82017-08-23 16:32:03 -07005990static bool vmx_rdseed_supported(void)
5991{
5992 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005993 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005994}
5995
Paolo Bonzini80154d72017-08-24 13:55:35 +02005996static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005997{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005998 struct kvm_vcpu *vcpu = &vmx->vcpu;
5999
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006000 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006001
Paolo Bonzini80154d72017-08-24 13:55:35 +02006002 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006003 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6004 if (vmx->vpid == 0)
6005 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6006 if (!enable_ept) {
6007 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6008 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006009 /* Enable INVPCID for non-ept guests may cause performance regression. */
6010 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006011 }
6012 if (!enable_unrestricted_guest)
6013 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006014 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006015 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006016 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006017 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6018 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006019 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006020
6021 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6022 * in vmx_set_cr4. */
6023 exec_control &= ~SECONDARY_EXEC_DESC;
6024
Abel Gordonabc4fc52013-04-18 14:35:25 +03006025 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6026 (handle_vmptrld).
6027 We can NOT enable shadow_vmcs here because we don't have yet
6028 a current VMCS12
6029 */
6030 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006031
6032 if (!enable_pml)
6033 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006034
Paolo Bonzini3db13482017-08-24 14:48:03 +02006035 if (vmx_xsaves_supported()) {
6036 /* Exposing XSAVES only when XSAVE is exposed */
6037 bool xsaves_enabled =
6038 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6039 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6040
6041 if (!xsaves_enabled)
6042 exec_control &= ~SECONDARY_EXEC_XSAVES;
6043
6044 if (nested) {
6045 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006046 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006047 SECONDARY_EXEC_XSAVES;
6048 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006049 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006050 ~SECONDARY_EXEC_XSAVES;
6051 }
6052 }
6053
Paolo Bonzini80154d72017-08-24 13:55:35 +02006054 if (vmx_rdtscp_supported()) {
6055 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6056 if (!rdtscp_enabled)
6057 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6058
6059 if (nested) {
6060 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006061 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006062 SECONDARY_EXEC_RDTSCP;
6063 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006064 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006065 ~SECONDARY_EXEC_RDTSCP;
6066 }
6067 }
6068
6069 if (vmx_invpcid_supported()) {
6070 /* Exposing INVPCID only when PCID is exposed */
6071 bool invpcid_enabled =
6072 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6073 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6074
6075 if (!invpcid_enabled) {
6076 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6077 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6078 }
6079
6080 if (nested) {
6081 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006082 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006083 SECONDARY_EXEC_ENABLE_INVPCID;
6084 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006085 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006086 ~SECONDARY_EXEC_ENABLE_INVPCID;
6087 }
6088 }
6089
Jim Mattson45ec3682017-08-23 16:32:04 -07006090 if (vmx_rdrand_supported()) {
6091 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6092 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006093 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006094
6095 if (nested) {
6096 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006097 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006098 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006099 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006100 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006101 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006102 }
6103 }
6104
Jim Mattson75f4fc82017-08-23 16:32:03 -07006105 if (vmx_rdseed_supported()) {
6106 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6107 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006108 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006109
6110 if (nested) {
6111 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006112 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006113 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006114 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006115 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006116 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006117 }
6118 }
6119
Paolo Bonzini80154d72017-08-24 13:55:35 +02006120 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006121}
6122
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006123static void ept_set_mmio_spte_mask(void)
6124{
6125 /*
6126 * EPT Misconfigurations can be generated if the value of bits 2:0
6127 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006128 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006129 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6130 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006131}
6132
Wanpeng Lif53cd632014-12-02 19:14:58 +08006133#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006134/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006135 * Sets up the vmcs for emulated real mode.
6136 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006137static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006138{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006139#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006140 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006141#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006142 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006143
Abel Gordon4607c2d2013-04-18 14:35:55 +03006144 if (enable_shadow_vmcs) {
6145 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
6146 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
6147 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006148 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006149 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006150
Avi Kivity6aa8b732006-12-10 02:21:36 -08006151 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6152
Avi Kivity6aa8b732006-12-10 02:21:36 -08006153 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006154 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006155 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006156
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006157 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006158
Dan Williamsdfa169b2016-06-02 11:17:24 -07006159 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006160 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006161 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006162 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006163 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006164
Andrey Smetanind62caab2015-11-10 15:36:33 +03006165 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006166 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6167 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6168 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6169 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6170
6171 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006172
Li RongQing0bcf2612015-12-03 13:29:34 +08006173 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006174 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006175 }
6176
Wanpeng Lib31c1142018-03-12 04:53:04 -07006177 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006178 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006179 vmx->ple_window = ple_window;
6180 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006181 }
6182
Xiao Guangrongc3707952011-07-12 03:28:04 +08006183 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6184 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006185 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6186
Avi Kivity9581d442010-10-19 16:46:55 +02006187 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6188 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006189 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006190#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006191 rdmsrl(MSR_FS_BASE, a);
6192 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6193 rdmsrl(MSR_GS_BASE, a);
6194 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6195#else
6196 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6197 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6198#endif
6199
Bandan Das2a499e42017-08-03 15:54:41 -04006200 if (cpu_has_vmx_vmfunc())
6201 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6202
Eddie Dong2cc51562007-05-21 07:28:09 +03006203 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6204 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006205 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006206 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006207 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006208
Radim Krčmář74545702015-04-27 15:11:25 +02006209 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6210 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006211
Paolo Bonzini03916db2014-07-24 14:21:57 +02006212 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006213 u32 index = vmx_msr_index[i];
6214 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006215 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006216
6217 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6218 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006219 if (wrmsr_safe(index, data_low, data_high) < 0)
6220 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006221 vmx->guest_msrs[j].index = i;
6222 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006223 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006224 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006225 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006226
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006227 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6228 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006229
6230 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006231
6232 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006233 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006234
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006235 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6236 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6237
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006238 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006239
Wanpeng Lif53cd632014-12-02 19:14:58 +08006240 if (vmx_xsaves_supported())
6241 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6242
Peter Feiner4e595162016-07-07 14:49:58 -07006243 if (enable_pml) {
6244 ASSERT(vmx->pml_pg);
6245 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6246 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6247 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006248}
6249
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006250static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006251{
6252 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006253 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006254 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006255
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006256 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006257 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006258
Wanpeng Li518e7b92018-02-28 14:03:31 +08006259 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006260 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006261 kvm_set_cr8(vcpu, 0);
6262
6263 if (!init_event) {
6264 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6265 MSR_IA32_APICBASE_ENABLE;
6266 if (kvm_vcpu_is_reset_bsp(vcpu))
6267 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6268 apic_base_msr.host_initiated = true;
6269 kvm_set_apic_base(vcpu, &apic_base_msr);
6270 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006271
Avi Kivity2fb92db2011-04-27 19:42:18 +03006272 vmx_segment_cache_clear(vmx);
6273
Avi Kivity5706be02008-08-20 15:07:31 +03006274 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006275 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006276 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006277
6278 seg_setup(VCPU_SREG_DS);
6279 seg_setup(VCPU_SREG_ES);
6280 seg_setup(VCPU_SREG_FS);
6281 seg_setup(VCPU_SREG_GS);
6282 seg_setup(VCPU_SREG_SS);
6283
6284 vmcs_write16(GUEST_TR_SELECTOR, 0);
6285 vmcs_writel(GUEST_TR_BASE, 0);
6286 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6287 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6288
6289 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6290 vmcs_writel(GUEST_LDTR_BASE, 0);
6291 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6292 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6293
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006294 if (!init_event) {
6295 vmcs_write32(GUEST_SYSENTER_CS, 0);
6296 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6297 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6298 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6299 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006300
Wanpeng Lic37c2872017-11-20 14:52:21 -08006301 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006302 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006303
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006304 vmcs_writel(GUEST_GDTR_BASE, 0);
6305 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6306
6307 vmcs_writel(GUEST_IDTR_BASE, 0);
6308 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6309
Anthony Liguori443381a2010-12-06 10:53:38 -06006310 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006311 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006312 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006313 if (kvm_mpx_supported())
6314 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006315
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006316 setup_msrs(vmx);
6317
Avi Kivity6aa8b732006-12-10 02:21:36 -08006318 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6319
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006320 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006321 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006322 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006323 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006324 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006325 vmcs_write32(TPR_THRESHOLD, 0);
6326 }
6327
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006328 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006329
Sheng Yang2384d2b2008-01-17 15:14:33 +08006330 if (vmx->vpid != 0)
6331 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6332
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006333 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006334 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006335 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006336 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006337 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006338
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006339 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006340
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006341 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006342 if (init_event)
6343 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006344}
6345
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006346/*
6347 * In nested virtualization, check if L1 asked to exit on external interrupts.
6348 * For most existing hypervisors, this will always return true.
6349 */
6350static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6351{
6352 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6353 PIN_BASED_EXT_INTR_MASK;
6354}
6355
Bandan Das77b0f5d2014-04-19 18:17:45 -04006356/*
6357 * In nested virtualization, check if L1 has set
6358 * VM_EXIT_ACK_INTR_ON_EXIT
6359 */
6360static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6361{
6362 return get_vmcs12(vcpu)->vm_exit_controls &
6363 VM_EXIT_ACK_INTR_ON_EXIT;
6364}
6365
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006366static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6367{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006368 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006369}
6370
Jan Kiszkac9a79532014-03-07 20:03:15 +01006371static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006372{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006373 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6374 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006375}
6376
Jan Kiszkac9a79532014-03-07 20:03:15 +01006377static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006378{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006379 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006380 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006381 enable_irq_window(vcpu);
6382 return;
6383 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006384
Paolo Bonzini47c01522016-12-19 11:44:07 +01006385 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6386 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006387}
6388
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006389static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006390{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006391 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006392 uint32_t intr;
6393 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006394
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006395 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006396
Avi Kivityfa89a812008-09-01 15:57:51 +03006397 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006398 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006399 int inc_eip = 0;
6400 if (vcpu->arch.interrupt.soft)
6401 inc_eip = vcpu->arch.event_exit_inst_len;
6402 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006403 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006404 return;
6405 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006406 intr = irq | INTR_INFO_VALID_MASK;
6407 if (vcpu->arch.interrupt.soft) {
6408 intr |= INTR_TYPE_SOFT_INTR;
6409 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6410 vmx->vcpu.arch.event_exit_inst_len);
6411 } else
6412 intr |= INTR_TYPE_EXT_INTR;
6413 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006414
6415 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006416}
6417
Sheng Yangf08864b2008-05-15 18:23:25 +08006418static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6419{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006420 struct vcpu_vmx *vmx = to_vmx(vcpu);
6421
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006422 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006423 /*
6424 * Tracking the NMI-blocked state in software is built upon
6425 * finding the next open IRQ window. This, in turn, depends on
6426 * well-behaving guests: They have to keep IRQs disabled at
6427 * least as long as the NMI handler runs. Otherwise we may
6428 * cause NMI nesting, maybe breaking the guest. But as this is
6429 * highly unlikely, we can live with the residual risk.
6430 */
6431 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6432 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6433 }
6434
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006435 ++vcpu->stat.nmi_injections;
6436 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006437
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006438 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006439 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006440 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006441 return;
6442 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006443
Sheng Yangf08864b2008-05-15 18:23:25 +08006444 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6445 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006446
6447 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006448}
6449
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006450static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6451{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006452 struct vcpu_vmx *vmx = to_vmx(vcpu);
6453 bool masked;
6454
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006455 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006456 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006457 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006458 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006459 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6460 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6461 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006462}
6463
6464static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6465{
6466 struct vcpu_vmx *vmx = to_vmx(vcpu);
6467
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006468 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006469 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6470 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6471 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6472 }
6473 } else {
6474 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6475 if (masked)
6476 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6477 GUEST_INTR_STATE_NMI);
6478 else
6479 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6480 GUEST_INTR_STATE_NMI);
6481 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006482}
6483
Jan Kiszka2505dc92013-04-14 12:12:47 +02006484static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6485{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006486 if (to_vmx(vcpu)->nested.nested_run_pending)
6487 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006488
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006489 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006490 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6491 return 0;
6492
Jan Kiszka2505dc92013-04-14 12:12:47 +02006493 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6494 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6495 | GUEST_INTR_STATE_NMI));
6496}
6497
Gleb Natapov78646122009-03-23 12:12:11 +02006498static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6499{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006500 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6501 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006502 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6503 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006504}
6505
Izik Eiduscbc94022007-10-25 00:29:55 +02006506static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6507{
6508 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006509
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006510 if (enable_unrestricted_guest)
6511 return 0;
6512
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006513 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6514 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006515 if (ret)
6516 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006517 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006518 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006519}
6520
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006521static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6522{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006523 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006524 return 0;
6525}
6526
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006527static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006528{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006529 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006530 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006531 /*
6532 * Update instruction length as we may reinject the exception
6533 * from user space while in guest debugging mode.
6534 */
6535 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6536 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006537 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006538 return false;
6539 /* fall through */
6540 case DB_VECTOR:
6541 if (vcpu->guest_debug &
6542 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6543 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006544 /* fall through */
6545 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006546 case OF_VECTOR:
6547 case BR_VECTOR:
6548 case UD_VECTOR:
6549 case DF_VECTOR:
6550 case SS_VECTOR:
6551 case GP_VECTOR:
6552 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006553 return true;
6554 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006555 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006556 return false;
6557}
6558
6559static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6560 int vec, u32 err_code)
6561{
6562 /*
6563 * Instruction with address size override prefix opcode 0x67
6564 * Cause the #SS fault with 0 error code in VM86 mode.
6565 */
6566 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6567 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6568 if (vcpu->arch.halt_request) {
6569 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006570 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006571 }
6572 return 1;
6573 }
6574 return 0;
6575 }
6576
6577 /*
6578 * Forward all other exceptions that are valid in real mode.
6579 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6580 * the required debugging infrastructure rework.
6581 */
6582 kvm_queue_exception(vcpu, vec);
6583 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006584}
6585
Andi Kleena0861c02009-06-08 17:37:09 +08006586/*
6587 * Trigger machine check on the host. We assume all the MSRs are already set up
6588 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6589 * We pass a fake environment to the machine check handler because we want
6590 * the guest to be always treated like user space, no matter what context
6591 * it used internally.
6592 */
6593static void kvm_machine_check(void)
6594{
6595#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6596 struct pt_regs regs = {
6597 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6598 .flags = X86_EFLAGS_IF,
6599 };
6600
6601 do_machine_check(&regs, 0);
6602#endif
6603}
6604
Avi Kivity851ba692009-08-24 11:10:17 +03006605static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006606{
6607 /* already handled by vcpu_run */
6608 return 1;
6609}
6610
Avi Kivity851ba692009-08-24 11:10:17 +03006611static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006612{
Avi Kivity1155f762007-11-22 11:30:47 +02006613 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006614 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006615 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006616 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006617 u32 vect_info;
6618 enum emulation_result er;
6619
Avi Kivity1155f762007-11-22 11:30:47 +02006620 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006621 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006622
Andi Kleena0861c02009-06-08 17:37:09 +08006623 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006624 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006625
Jim Mattsonef85b672016-12-12 11:01:37 -08006626 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006627 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006628
Wanpeng Li082d06e2018-04-03 16:28:48 -07006629 if (is_invalid_opcode(intr_info))
6630 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006631
Avi Kivity6aa8b732006-12-10 02:21:36 -08006632 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006633 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006634 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006635
Liran Alon9e869482018-03-12 13:12:51 +02006636 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6637 WARN_ON_ONCE(!enable_vmware_backdoor);
6638 er = emulate_instruction(vcpu,
6639 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6640 if (er == EMULATE_USER_EXIT)
6641 return 0;
6642 else if (er != EMULATE_DONE)
6643 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6644 return 1;
6645 }
6646
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006647 /*
6648 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6649 * MMIO, it is better to report an internal error.
6650 * See the comments in vmx_handle_exit.
6651 */
6652 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6653 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6654 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6655 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006656 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006657 vcpu->run->internal.data[0] = vect_info;
6658 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006659 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006660 return 0;
6661 }
6662
Avi Kivity6aa8b732006-12-10 02:21:36 -08006663 if (is_page_fault(intr_info)) {
6664 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006665 /* EPT won't cause page fault directly */
6666 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006667 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006668 }
6669
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006670 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006671
6672 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6673 return handle_rmode_exception(vcpu, ex_no, error_code);
6674
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006675 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006676 case AC_VECTOR:
6677 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6678 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006679 case DB_VECTOR:
6680 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6681 if (!(vcpu->guest_debug &
6682 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006683 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006684 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006685 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006686 skip_emulated_instruction(vcpu);
6687
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006688 kvm_queue_exception(vcpu, DB_VECTOR);
6689 return 1;
6690 }
6691 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6692 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6693 /* fall through */
6694 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006695 /*
6696 * Update instruction length as we may reinject #BP from
6697 * user space while in guest debugging mode. Reading it for
6698 * #DB as well causes no harm, it is not used in that case.
6699 */
6700 vmx->vcpu.arch.event_exit_inst_len =
6701 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006702 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006703 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006704 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6705 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006706 break;
6707 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006708 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6709 kvm_run->ex.exception = ex_no;
6710 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006711 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006712 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006713 return 0;
6714}
6715
Avi Kivity851ba692009-08-24 11:10:17 +03006716static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006717{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006718 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006719 return 1;
6720}
6721
Avi Kivity851ba692009-08-24 11:10:17 +03006722static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006723{
Avi Kivity851ba692009-08-24 11:10:17 +03006724 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006725 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006726 return 0;
6727}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006728
Avi Kivity851ba692009-08-24 11:10:17 +03006729static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006730{
He, Qingbfdaab02007-09-12 14:18:28 +08006731 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006732 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006733 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006734
He, Qingbfdaab02007-09-12 14:18:28 +08006735 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006736 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006737
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006738 ++vcpu->stat.io_exits;
6739
Sean Christopherson432baf62018-03-08 08:57:26 -08006740 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006741 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006742
6743 port = exit_qualification >> 16;
6744 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006745 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006746
Sean Christophersondca7f122018-03-08 08:57:27 -08006747 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006748}
6749
Ingo Molnar102d8322007-02-19 14:37:47 +02006750static void
6751vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6752{
6753 /*
6754 * Patch in the VMCALL instruction:
6755 */
6756 hypercall[0] = 0x0f;
6757 hypercall[1] = 0x01;
6758 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006759}
6760
Guo Chao0fa06072012-06-28 15:16:19 +08006761/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006762static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6763{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006764 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006765 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6766 unsigned long orig_val = val;
6767
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006768 /*
6769 * We get here when L2 changed cr0 in a way that did not change
6770 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006771 * but did change L0 shadowed bits. So we first calculate the
6772 * effective cr0 value that L1 would like to write into the
6773 * hardware. It consists of the L2-owned bits from the new
6774 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006775 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006776 val = (val & ~vmcs12->cr0_guest_host_mask) |
6777 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6778
David Matlack38991522016-11-29 18:14:08 -08006779 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006780 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006781
6782 if (kvm_set_cr0(vcpu, val))
6783 return 1;
6784 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006785 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006786 } else {
6787 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006788 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006789 return 1;
David Matlack38991522016-11-29 18:14:08 -08006790
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006791 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006792 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006793}
6794
6795static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6796{
6797 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006798 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6799 unsigned long orig_val = val;
6800
6801 /* analogously to handle_set_cr0 */
6802 val = (val & ~vmcs12->cr4_guest_host_mask) |
6803 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6804 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006805 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006806 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006807 return 0;
6808 } else
6809 return kvm_set_cr4(vcpu, val);
6810}
6811
Paolo Bonzini0367f202016-07-12 10:44:55 +02006812static int handle_desc(struct kvm_vcpu *vcpu)
6813{
6814 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6815 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6816}
6817
Avi Kivity851ba692009-08-24 11:10:17 +03006818static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006819{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006820 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006821 int cr;
6822 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006823 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006824 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006825
He, Qingbfdaab02007-09-12 14:18:28 +08006826 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006827 cr = exit_qualification & 15;
6828 reg = (exit_qualification >> 8) & 15;
6829 switch ((exit_qualification >> 4) & 3) {
6830 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006831 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006832 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006833 switch (cr) {
6834 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006835 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006836 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006837 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006838 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006839 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006840 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006841 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006842 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006843 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006844 case 8: {
6845 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006846 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006847 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006848 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006849 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006850 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006851 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006852 return ret;
6853 /*
6854 * TODO: we might be squashing a
6855 * KVM_GUESTDBG_SINGLESTEP-triggered
6856 * KVM_EXIT_DEBUG here.
6857 */
Avi Kivity851ba692009-08-24 11:10:17 +03006858 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006859 return 0;
6860 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006861 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006862 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006863 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006864 WARN_ONCE(1, "Guest should always own CR0.TS");
6865 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006866 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006867 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006868 case 1: /*mov from cr*/
6869 switch (cr) {
6870 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006871 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006872 val = kvm_read_cr3(vcpu);
6873 kvm_register_write(vcpu, reg, val);
6874 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006875 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006876 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006877 val = kvm_get_cr8(vcpu);
6878 kvm_register_write(vcpu, reg, val);
6879 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006880 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006881 }
6882 break;
6883 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006884 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006885 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006886 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006887
Kyle Huey6affcbe2016-11-29 12:40:40 -08006888 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006889 default:
6890 break;
6891 }
Avi Kivity851ba692009-08-24 11:10:17 +03006892 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006893 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006894 (int)(exit_qualification >> 4) & 3, cr);
6895 return 0;
6896}
6897
Avi Kivity851ba692009-08-24 11:10:17 +03006898static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006899{
He, Qingbfdaab02007-09-12 14:18:28 +08006900 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006901 int dr, dr7, reg;
6902
6903 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6904 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6905
6906 /* First, if DR does not exist, trigger UD */
6907 if (!kvm_require_dr(vcpu, dr))
6908 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006909
Jan Kiszkaf2483412010-01-20 18:20:20 +01006910 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006911 if (!kvm_require_cpl(vcpu, 0))
6912 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006913 dr7 = vmcs_readl(GUEST_DR7);
6914 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006915 /*
6916 * As the vm-exit takes precedence over the debug trap, we
6917 * need to emulate the latter, either for the host or the
6918 * guest debugging itself.
6919 */
6920 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006921 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006922 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006923 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006924 vcpu->run->debug.arch.exception = DB_VECTOR;
6925 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006926 return 0;
6927 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006928 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006929 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006930 kvm_queue_exception(vcpu, DB_VECTOR);
6931 return 1;
6932 }
6933 }
6934
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006935 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006936 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6937 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006938
6939 /*
6940 * No more DR vmexits; force a reload of the debug registers
6941 * and reenter on this instruction. The next vmexit will
6942 * retrieve the full state of the debug registers.
6943 */
6944 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6945 return 1;
6946 }
6947
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006948 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6949 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006950 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006951
6952 if (kvm_get_dr(vcpu, dr, &val))
6953 return 1;
6954 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006955 } else
Nadav Amit57773922014-06-18 17:19:23 +03006956 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006957 return 1;
6958
Kyle Huey6affcbe2016-11-29 12:40:40 -08006959 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006960}
6961
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006962static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6963{
6964 return vcpu->arch.dr6;
6965}
6966
6967static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6968{
6969}
6970
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006971static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6972{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006973 get_debugreg(vcpu->arch.db[0], 0);
6974 get_debugreg(vcpu->arch.db[1], 1);
6975 get_debugreg(vcpu->arch.db[2], 2);
6976 get_debugreg(vcpu->arch.db[3], 3);
6977 get_debugreg(vcpu->arch.dr6, 6);
6978 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6979
6980 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006981 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006982}
6983
Gleb Natapov020df072010-04-13 10:05:23 +03006984static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6985{
6986 vmcs_writel(GUEST_DR7, val);
6987}
6988
Avi Kivity851ba692009-08-24 11:10:17 +03006989static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006990{
Kyle Huey6a908b62016-11-29 12:40:37 -08006991 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006992}
6993
Avi Kivity851ba692009-08-24 11:10:17 +03006994static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006995{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006996 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006997 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006998
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006999 msr_info.index = ecx;
7000 msr_info.host_initiated = false;
7001 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007002 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007003 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007004 return 1;
7005 }
7006
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007007 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007008
Avi Kivity6aa8b732006-12-10 02:21:36 -08007009 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007010 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7011 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007012 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007013}
7014
Avi Kivity851ba692009-08-24 11:10:17 +03007015static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007016{
Will Auld8fe8ab42012-11-29 12:42:12 -08007017 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007018 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7019 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7020 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007021
Will Auld8fe8ab42012-11-29 12:42:12 -08007022 msr.data = data;
7023 msr.index = ecx;
7024 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007025 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007026 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007027 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007028 return 1;
7029 }
7030
Avi Kivity59200272010-01-25 19:47:02 +02007031 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007032 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007033}
7034
Avi Kivity851ba692009-08-24 11:10:17 +03007035static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007036{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007037 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007038 return 1;
7039}
7040
Avi Kivity851ba692009-08-24 11:10:17 +03007041static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007042{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007043 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7044 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007045
Avi Kivity3842d132010-07-27 12:30:24 +03007046 kvm_make_request(KVM_REQ_EVENT, vcpu);
7047
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007048 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007049 return 1;
7050}
7051
Avi Kivity851ba692009-08-24 11:10:17 +03007052static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007053{
Avi Kivityd3bef152007-06-05 15:53:05 +03007054 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007055}
7056
Avi Kivity851ba692009-08-24 11:10:17 +03007057static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007058{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007059 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007060}
7061
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007062static int handle_invd(struct kvm_vcpu *vcpu)
7063{
Andre Przywara51d8b662010-12-21 11:12:02 +01007064 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007065}
7066
Avi Kivity851ba692009-08-24 11:10:17 +03007067static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007068{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007069 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007070
7071 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007072 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007073}
7074
Avi Kivityfee84b02011-11-10 14:57:25 +02007075static int handle_rdpmc(struct kvm_vcpu *vcpu)
7076{
7077 int err;
7078
7079 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007080 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007081}
7082
Avi Kivity851ba692009-08-24 11:10:17 +03007083static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007084{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007085 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007086}
7087
Dexuan Cui2acf9232010-06-10 11:27:12 +08007088static int handle_xsetbv(struct kvm_vcpu *vcpu)
7089{
7090 u64 new_bv = kvm_read_edx_eax(vcpu);
7091 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7092
7093 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007094 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007095 return 1;
7096}
7097
Wanpeng Lif53cd632014-12-02 19:14:58 +08007098static int handle_xsaves(struct kvm_vcpu *vcpu)
7099{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007100 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007101 WARN(1, "this should never happen\n");
7102 return 1;
7103}
7104
7105static int handle_xrstors(struct kvm_vcpu *vcpu)
7106{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007107 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007108 WARN(1, "this should never happen\n");
7109 return 1;
7110}
7111
Avi Kivity851ba692009-08-24 11:10:17 +03007112static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007113{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007114 if (likely(fasteoi)) {
7115 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7116 int access_type, offset;
7117
7118 access_type = exit_qualification & APIC_ACCESS_TYPE;
7119 offset = exit_qualification & APIC_ACCESS_OFFSET;
7120 /*
7121 * Sane guest uses MOV to write EOI, with written value
7122 * not cared. So make a short-circuit here by avoiding
7123 * heavy instruction emulation.
7124 */
7125 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7126 (offset == APIC_EOI)) {
7127 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007128 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007129 }
7130 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007131 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007132}
7133
Yang Zhangc7c9c562013-01-25 10:18:51 +08007134static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7135{
7136 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7137 int vector = exit_qualification & 0xff;
7138
7139 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7140 kvm_apic_set_eoi_accelerated(vcpu, vector);
7141 return 1;
7142}
7143
Yang Zhang83d4c282013-01-25 10:18:49 +08007144static int handle_apic_write(struct kvm_vcpu *vcpu)
7145{
7146 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7147 u32 offset = exit_qualification & 0xfff;
7148
7149 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7150 kvm_apic_write_nodecode(vcpu, offset);
7151 return 1;
7152}
7153
Avi Kivity851ba692009-08-24 11:10:17 +03007154static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007155{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007156 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007157 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007158 bool has_error_code = false;
7159 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007160 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007161 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007162
7163 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007164 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007165 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007166
7167 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7168
7169 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007170 if (reason == TASK_SWITCH_GATE && idt_v) {
7171 switch (type) {
7172 case INTR_TYPE_NMI_INTR:
7173 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007174 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007175 break;
7176 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007177 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007178 kvm_clear_interrupt_queue(vcpu);
7179 break;
7180 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007181 if (vmx->idt_vectoring_info &
7182 VECTORING_INFO_DELIVER_CODE_MASK) {
7183 has_error_code = true;
7184 error_code =
7185 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7186 }
7187 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007188 case INTR_TYPE_SOFT_EXCEPTION:
7189 kvm_clear_exception_queue(vcpu);
7190 break;
7191 default:
7192 break;
7193 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007194 }
Izik Eidus37817f22008-03-24 23:14:53 +02007195 tss_selector = exit_qualification;
7196
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007197 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7198 type != INTR_TYPE_EXT_INTR &&
7199 type != INTR_TYPE_NMI_INTR))
7200 skip_emulated_instruction(vcpu);
7201
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007202 if (kvm_task_switch(vcpu, tss_selector,
7203 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7204 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007205 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7206 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7207 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007208 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007209 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007210
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007211 /*
7212 * TODO: What about debug traps on tss switch?
7213 * Are we supposed to inject them and update dr6?
7214 */
7215
7216 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007217}
7218
Avi Kivity851ba692009-08-24 11:10:17 +03007219static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007220{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007221 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007222 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007223 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007224
Sheng Yangf9c617f2009-03-25 10:08:52 +08007225 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007226
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007227 /*
7228 * EPT violation happened while executing iret from NMI,
7229 * "blocked by NMI" bit has to be set before next VM entry.
7230 * There are errata that may cause this bit to not be set:
7231 * AAK134, BY25.
7232 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007233 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007234 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007235 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007236 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7237
Sheng Yang14394422008-04-28 12:24:45 +08007238 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007239 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007240
Junaid Shahid27959a42016-12-06 16:46:10 -08007241 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007242 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007243 ? PFERR_USER_MASK : 0;
7244 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007245 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007246 ? PFERR_WRITE_MASK : 0;
7247 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007248 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007249 ? PFERR_FETCH_MASK : 0;
7250 /* ept page table entry is present? */
7251 error_code |= (exit_qualification &
7252 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7253 EPT_VIOLATION_EXECUTABLE))
7254 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007255
Paolo Bonzinieebed242016-11-28 14:39:58 +01007256 error_code |= (exit_qualification & 0x100) != 0 ?
7257 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007258
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007259 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007260 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007261}
7262
Avi Kivity851ba692009-08-24 11:10:17 +03007263static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007264{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007265 gpa_t gpa;
7266
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007267 /*
7268 * A nested guest cannot optimize MMIO vmexits, because we have an
7269 * nGPA here instead of the required GPA.
7270 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007271 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007272 if (!is_guest_mode(vcpu) &&
7273 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007274 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007275 /*
7276 * Doing kvm_skip_emulated_instruction() depends on undefined
7277 * behavior: Intel's manual doesn't mandate
7278 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7279 * occurs and while on real hardware it was observed to be set,
7280 * other hypervisors (namely Hyper-V) don't set it, we end up
7281 * advancing IP with some random value. Disable fast mmio when
7282 * running nested and keep it for real hardware in hope that
7283 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7284 */
7285 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7286 return kvm_skip_emulated_instruction(vcpu);
7287 else
7288 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7289 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007290 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007291
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007292 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007293}
7294
Avi Kivity851ba692009-08-24 11:10:17 +03007295static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007296{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007297 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007298 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7299 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007300 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007301 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007302
7303 return 1;
7304}
7305
Mohammed Gamal80ced182009-09-01 12:48:18 +02007306static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007307{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007308 struct vcpu_vmx *vmx = to_vmx(vcpu);
7309 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007310 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007311 u32 cpu_exec_ctrl;
7312 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007313 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007314
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007315 /*
7316 * We should never reach the point where we are emulating L2
7317 * due to invalid guest state as that means we incorrectly
7318 * allowed a nested VMEntry with an invalid vmcs12.
7319 */
7320 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7321
Avi Kivity49e9d552010-09-19 14:34:08 +02007322 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7323 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007324
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007325 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007326 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007327 return handle_interrupt_window(&vmx->vcpu);
7328
Radim Krčmář72875d82017-04-26 22:32:19 +02007329 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007330 return 1;
7331
Liran Alon9b8ae632017-11-05 16:56:34 +02007332 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007333
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007334 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007335 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007336 ret = 0;
7337 goto out;
7338 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007339
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007340 if (err != EMULATE_DONE)
7341 goto emulation_error;
7342
7343 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7344 vcpu->arch.exception.pending)
7345 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007346
Gleb Natapov8d76c492013-05-08 18:38:44 +03007347 if (vcpu->arch.halt_request) {
7348 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007349 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007350 goto out;
7351 }
7352
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007353 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007354 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007355 if (need_resched())
7356 schedule();
7357 }
7358
Mohammed Gamal80ced182009-09-01 12:48:18 +02007359out:
7360 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007361
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007362emulation_error:
7363 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7364 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7365 vcpu->run->internal.ndata = 0;
7366 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007367}
7368
7369static void grow_ple_window(struct kvm_vcpu *vcpu)
7370{
7371 struct vcpu_vmx *vmx = to_vmx(vcpu);
7372 int old = vmx->ple_window;
7373
Babu Mogerc8e88712018-03-16 16:37:24 -04007374 vmx->ple_window = __grow_ple_window(old, ple_window,
7375 ple_window_grow,
7376 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007377
7378 if (vmx->ple_window != old)
7379 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007380
7381 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007382}
7383
7384static void shrink_ple_window(struct kvm_vcpu *vcpu)
7385{
7386 struct vcpu_vmx *vmx = to_vmx(vcpu);
7387 int old = vmx->ple_window;
7388
Babu Mogerc8e88712018-03-16 16:37:24 -04007389 vmx->ple_window = __shrink_ple_window(old, ple_window,
7390 ple_window_shrink,
7391 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007392
7393 if (vmx->ple_window != old)
7394 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007395
7396 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007397}
7398
7399/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007400 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7401 */
7402static void wakeup_handler(void)
7403{
7404 struct kvm_vcpu *vcpu;
7405 int cpu = smp_processor_id();
7406
7407 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7408 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7409 blocked_vcpu_list) {
7410 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7411
7412 if (pi_test_on(pi_desc) == 1)
7413 kvm_vcpu_kick(vcpu);
7414 }
7415 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7416}
7417
Peng Haoe01bca22018-04-07 05:47:32 +08007418static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007419{
7420 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7421 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7422 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7423 0ull, VMX_EPT_EXECUTABLE_MASK,
7424 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007425 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007426
7427 ept_set_mmio_spte_mask();
7428 kvm_enable_tdp();
7429}
7430
Tiejun Chenf2c76482014-10-28 10:14:47 +08007431static __init int hardware_setup(void)
7432{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007433 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007434
7435 rdmsrl_safe(MSR_EFER, &host_efer);
7436
7437 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7438 kvm_define_shared_msr(i, vmx_msr_index[i]);
7439
Radim Krčmář23611332016-09-29 22:41:33 +02007440 for (i = 0; i < VMX_BITMAP_NR; i++) {
7441 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7442 if (!vmx_bitmap[i])
7443 goto out;
7444 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007445
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007446 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7447 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7448
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007449 if (setup_vmcs_config(&vmcs_config) < 0) {
7450 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007451 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007452 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007453
7454 if (boot_cpu_has(X86_FEATURE_NX))
7455 kvm_enable_efer_bits(EFER_NX);
7456
Wanpeng Li08d839c2017-03-23 05:30:08 -07007457 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7458 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007459 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007460
Tiejun Chenf2c76482014-10-28 10:14:47 +08007461 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007462 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007463 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007464 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007465 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007466
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007467 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007468 enable_ept_ad_bits = 0;
7469
Wanpeng Li8ad81822017-10-09 15:51:53 -07007470 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007471 enable_unrestricted_guest = 0;
7472
Paolo Bonziniad15a292015-01-30 16:18:49 +01007473 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007474 flexpriority_enabled = 0;
7475
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007476 if (!cpu_has_virtual_nmis())
7477 enable_vnmi = 0;
7478
Paolo Bonziniad15a292015-01-30 16:18:49 +01007479 /*
7480 * set_apic_access_page_addr() is used to reload apic access
7481 * page upon invalidation. No need to do anything if not
7482 * using the APIC_ACCESS_ADDR VMCS field.
7483 */
7484 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007485 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007486
7487 if (!cpu_has_vmx_tpr_shadow())
7488 kvm_x86_ops->update_cr8_intercept = NULL;
7489
7490 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7491 kvm_disable_largepages();
7492
Wanpeng Li0f107682017-09-28 18:06:24 -07007493 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007494 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007495 ple_window = 0;
7496 ple_window_grow = 0;
7497 ple_window_max = 0;
7498 ple_window_shrink = 0;
7499 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007500
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007501 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007502 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007503 kvm_x86_ops->sync_pir_to_irr = NULL;
7504 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007505
Haozhong Zhang64903d62015-10-20 15:39:09 +08007506 if (cpu_has_vmx_tsc_scaling()) {
7507 kvm_has_tsc_control = true;
7508 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7509 kvm_tsc_scaling_ratio_frac_bits = 48;
7510 }
7511
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007512 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7513
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007514 if (enable_ept)
7515 vmx_enable_tdp();
7516 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007517 kvm_disable_tdp();
7518
Kai Huang843e4332015-01-28 10:54:28 +08007519 /*
7520 * Only enable PML when hardware supports PML feature, and both EPT
7521 * and EPT A/D bit features are enabled -- PML depends on them to work.
7522 */
7523 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7524 enable_pml = 0;
7525
7526 if (!enable_pml) {
7527 kvm_x86_ops->slot_enable_log_dirty = NULL;
7528 kvm_x86_ops->slot_disable_log_dirty = NULL;
7529 kvm_x86_ops->flush_log_dirty = NULL;
7530 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7531 }
7532
Yunhong Jiang64672c92016-06-13 14:19:59 -07007533 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7534 u64 vmx_msr;
7535
7536 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7537 cpu_preemption_timer_multi =
7538 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7539 } else {
7540 kvm_x86_ops->set_hv_timer = NULL;
7541 kvm_x86_ops->cancel_hv_timer = NULL;
7542 }
7543
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007544 if (!cpu_has_vmx_shadow_vmcs())
7545 enable_shadow_vmcs = 0;
7546 if (enable_shadow_vmcs)
7547 init_vmcs_shadow_fields();
7548
Feng Wubf9f6ac2015-09-18 22:29:55 +08007549 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007550 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007551
Ashok Rajc45dcc72016-06-22 14:59:56 +08007552 kvm_mce_cap_supported |= MCG_LMCE_P;
7553
Tiejun Chenf2c76482014-10-28 10:14:47 +08007554 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007555
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007556out:
Radim Krčmář23611332016-09-29 22:41:33 +02007557 for (i = 0; i < VMX_BITMAP_NR; i++)
7558 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007559
7560 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007561}
7562
7563static __exit void hardware_unsetup(void)
7564{
Radim Krčmář23611332016-09-29 22:41:33 +02007565 int i;
7566
7567 for (i = 0; i < VMX_BITMAP_NR; i++)
7568 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007569
Tiejun Chenf2c76482014-10-28 10:14:47 +08007570 free_kvm_area();
7571}
7572
Avi Kivity6aa8b732006-12-10 02:21:36 -08007573/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007574 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7575 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7576 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007577static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007578{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007579 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007580 grow_ple_window(vcpu);
7581
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007582 /*
7583 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7584 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7585 * never set PAUSE_EXITING and just set PLE if supported,
7586 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7587 */
7588 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007589 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007590}
7591
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007592static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007593{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007594 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007595}
7596
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007597static int handle_mwait(struct kvm_vcpu *vcpu)
7598{
7599 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7600 return handle_nop(vcpu);
7601}
7602
Jim Mattson45ec3682017-08-23 16:32:04 -07007603static int handle_invalid_op(struct kvm_vcpu *vcpu)
7604{
7605 kvm_queue_exception(vcpu, UD_VECTOR);
7606 return 1;
7607}
7608
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007609static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7610{
7611 return 1;
7612}
7613
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007614static int handle_monitor(struct kvm_vcpu *vcpu)
7615{
7616 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7617 return handle_nop(vcpu);
7618}
7619
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007620/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007621 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7622 * set the success or error code of an emulated VMX instruction, as specified
7623 * by Vol 2B, VMX Instruction Reference, "Conventions".
7624 */
7625static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7626{
7627 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7628 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7629 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7630}
7631
7632static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7633{
7634 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7635 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7636 X86_EFLAGS_SF | X86_EFLAGS_OF))
7637 | X86_EFLAGS_CF);
7638}
7639
Abel Gordon145c28d2013-04-18 14:36:55 +03007640static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007641 u32 vm_instruction_error)
7642{
7643 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7644 /*
7645 * failValid writes the error number to the current VMCS, which
7646 * can't be done there isn't a current VMCS.
7647 */
7648 nested_vmx_failInvalid(vcpu);
7649 return;
7650 }
7651 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7652 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7653 X86_EFLAGS_SF | X86_EFLAGS_OF))
7654 | X86_EFLAGS_ZF);
7655 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7656 /*
7657 * We don't need to force a shadow sync because
7658 * VM_INSTRUCTION_ERROR is not shadowed
7659 */
7660}
Abel Gordon145c28d2013-04-18 14:36:55 +03007661
Wincy Vanff651cb2014-12-11 08:52:58 +03007662static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7663{
7664 /* TODO: not to reset guest simply here. */
7665 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007666 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007667}
7668
Jan Kiszkaf41245002014-03-07 20:03:13 +01007669static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7670{
7671 struct vcpu_vmx *vmx =
7672 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7673
7674 vmx->nested.preemption_timer_expired = true;
7675 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7676 kvm_vcpu_kick(&vmx->vcpu);
7677
7678 return HRTIMER_NORESTART;
7679}
7680
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007681/*
Bandan Das19677e32014-05-06 02:19:15 -04007682 * Decode the memory-address operand of a vmx instruction, as recorded on an
7683 * exit caused by such an instruction (run by a guest hypervisor).
7684 * On success, returns 0. When the operand is invalid, returns 1 and throws
7685 * #UD or #GP.
7686 */
7687static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7688 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007689 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007690{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007691 gva_t off;
7692 bool exn;
7693 struct kvm_segment s;
7694
Bandan Das19677e32014-05-06 02:19:15 -04007695 /*
7696 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7697 * Execution", on an exit, vmx_instruction_info holds most of the
7698 * addressing components of the operand. Only the displacement part
7699 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7700 * For how an actual address is calculated from all these components,
7701 * refer to Vol. 1, "Operand Addressing".
7702 */
7703 int scaling = vmx_instruction_info & 3;
7704 int addr_size = (vmx_instruction_info >> 7) & 7;
7705 bool is_reg = vmx_instruction_info & (1u << 10);
7706 int seg_reg = (vmx_instruction_info >> 15) & 7;
7707 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7708 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7709 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7710 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7711
7712 if (is_reg) {
7713 kvm_queue_exception(vcpu, UD_VECTOR);
7714 return 1;
7715 }
7716
7717 /* Addr = segment_base + offset */
7718 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007719 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007720 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007721 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007722 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007723 off += kvm_register_read(vcpu, index_reg)<<scaling;
7724 vmx_get_segment(vcpu, &s, seg_reg);
7725 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007726
7727 if (addr_size == 1) /* 32 bit */
7728 *ret &= 0xffffffff;
7729
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007730 /* Checks for #GP/#SS exceptions. */
7731 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007732 if (is_long_mode(vcpu)) {
7733 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7734 * non-canonical form. This is the only check on the memory
7735 * destination for long mode!
7736 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007737 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007738 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007739 /* Protected mode: apply checks for segment validity in the
7740 * following order:
7741 * - segment type check (#GP(0) may be thrown)
7742 * - usability check (#GP(0)/#SS(0))
7743 * - limit check (#GP(0)/#SS(0))
7744 */
7745 if (wr)
7746 /* #GP(0) if the destination operand is located in a
7747 * read-only data segment or any code segment.
7748 */
7749 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7750 else
7751 /* #GP(0) if the source operand is located in an
7752 * execute-only code segment
7753 */
7754 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007755 if (exn) {
7756 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7757 return 1;
7758 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007759 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7760 */
7761 exn = (s.unusable != 0);
7762 /* Protected mode: #GP(0)/#SS(0) if the memory
7763 * operand is outside the segment limit.
7764 */
7765 exn = exn || (off + sizeof(u64) > s.limit);
7766 }
7767 if (exn) {
7768 kvm_queue_exception_e(vcpu,
7769 seg_reg == VCPU_SREG_SS ?
7770 SS_VECTOR : GP_VECTOR,
7771 0);
7772 return 1;
7773 }
7774
Bandan Das19677e32014-05-06 02:19:15 -04007775 return 0;
7776}
7777
Radim Krčmářcbf71272017-05-19 15:48:51 +02007778static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007779{
7780 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007781 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007782
7783 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007784 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007785 return 1;
7786
Radim Krčmářcbf71272017-05-19 15:48:51 +02007787 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7788 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007789 kvm_inject_page_fault(vcpu, &e);
7790 return 1;
7791 }
7792
Bandan Das3573e222014-05-06 02:19:16 -04007793 return 0;
7794}
7795
Jim Mattsone29acc52016-11-30 12:03:43 -08007796static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7797{
7798 struct vcpu_vmx *vmx = to_vmx(vcpu);
7799 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007800 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007801
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007802 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7803 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007804 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007805
7806 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7807 if (!vmx->nested.cached_vmcs12)
7808 goto out_cached_vmcs12;
7809
7810 if (enable_shadow_vmcs) {
7811 shadow_vmcs = alloc_vmcs();
7812 if (!shadow_vmcs)
7813 goto out_shadow_vmcs;
7814 /* mark vmcs as shadow */
7815 shadow_vmcs->revision_id |= (1u << 31);
7816 /* init shadow vmcs */
7817 vmcs_clear(shadow_vmcs);
7818 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7819 }
7820
Jim Mattsone29acc52016-11-30 12:03:43 -08007821 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7822 HRTIMER_MODE_REL_PINNED);
7823 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7824
7825 vmx->nested.vmxon = true;
7826 return 0;
7827
7828out_shadow_vmcs:
7829 kfree(vmx->nested.cached_vmcs12);
7830
7831out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007832 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007833
Jim Mattsonde3a0022017-11-27 17:22:25 -06007834out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007835 return -ENOMEM;
7836}
7837
Bandan Das3573e222014-05-06 02:19:16 -04007838/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007839 * Emulate the VMXON instruction.
7840 * Currently, we just remember that VMX is active, and do not save or even
7841 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7842 * do not currently need to store anything in that guest-allocated memory
7843 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7844 * argument is different from the VMXON pointer (which the spec says they do).
7845 */
7846static int handle_vmon(struct kvm_vcpu *vcpu)
7847{
Jim Mattsone29acc52016-11-30 12:03:43 -08007848 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007849 gpa_t vmptr;
7850 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007851 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007852 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7853 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007854
Jim Mattson70f3aac2017-04-26 08:53:46 -07007855 /*
7856 * The Intel VMX Instruction Reference lists a bunch of bits that are
7857 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7858 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7859 * Otherwise, we should fail with #UD. But most faulting conditions
7860 * have already been checked by hardware, prior to the VM-exit for
7861 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7862 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007863 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007864 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007865 kvm_queue_exception(vcpu, UD_VECTOR);
7866 return 1;
7867 }
7868
Abel Gordon145c28d2013-04-18 14:36:55 +03007869 if (vmx->nested.vmxon) {
7870 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007871 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007872 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007873
Haozhong Zhang3b840802016-06-22 14:59:54 +08007874 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007875 != VMXON_NEEDED_FEATURES) {
7876 kvm_inject_gp(vcpu, 0);
7877 return 1;
7878 }
7879
Radim Krčmářcbf71272017-05-19 15:48:51 +02007880 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007881 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007882
7883 /*
7884 * SDM 3: 24.11.5
7885 * The first 4 bytes of VMXON region contain the supported
7886 * VMCS revision identifier
7887 *
7888 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7889 * which replaces physical address width with 32
7890 */
7891 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7892 nested_vmx_failInvalid(vcpu);
7893 return kvm_skip_emulated_instruction(vcpu);
7894 }
7895
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007896 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7897 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007898 nested_vmx_failInvalid(vcpu);
7899 return kvm_skip_emulated_instruction(vcpu);
7900 }
7901 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7902 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007903 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007904 nested_vmx_failInvalid(vcpu);
7905 return kvm_skip_emulated_instruction(vcpu);
7906 }
7907 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007908 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007909
7910 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007911 ret = enter_vmx_operation(vcpu);
7912 if (ret)
7913 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007914
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007915 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007916 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007917}
7918
7919/*
7920 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7921 * for running VMX instructions (except VMXON, whose prerequisites are
7922 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007923 * Note that many of these exceptions have priority over VM exits, so they
7924 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007925 */
7926static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7927{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007928 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007929 kvm_queue_exception(vcpu, UD_VECTOR);
7930 return 0;
7931 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007932 return 1;
7933}
7934
David Matlack8ca44e82017-08-01 14:00:39 -07007935static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7936{
7937 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7938 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7939}
7940
Abel Gordone7953d72013-04-18 14:37:55 +03007941static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7942{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007943 if (vmx->nested.current_vmptr == -1ull)
7944 return;
7945
Abel Gordon012f83c2013-04-18 14:39:25 +03007946 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007947 /* copy to memory all shadowed fields in case
7948 they were modified */
7949 copy_shadow_to_vmcs12(vmx);
7950 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007951 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007952 }
Wincy Van705699a2015-02-03 23:58:17 +08007953 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007954
7955 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007956 kvm_vcpu_write_guest_page(&vmx->vcpu,
7957 vmx->nested.current_vmptr >> PAGE_SHIFT,
7958 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007959
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007960 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007961}
7962
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007963/*
7964 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7965 * just stops using VMX.
7966 */
7967static void free_nested(struct vcpu_vmx *vmx)
7968{
Wanpeng Lib7455822017-11-22 14:04:00 -08007969 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007970 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007971
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007972 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007973 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007974 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007975 vmx->nested.posted_intr_nv = -1;
7976 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007977 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007978 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007979 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7980 free_vmcs(vmx->vmcs01.shadow_vmcs);
7981 vmx->vmcs01.shadow_vmcs = NULL;
7982 }
David Matlack4f2777b2016-07-13 17:16:37 -07007983 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007984 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007985 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007986 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007987 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007988 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007989 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007990 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007991 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007992 }
Wincy Van705699a2015-02-03 23:58:17 +08007993 if (vmx->nested.pi_desc_page) {
7994 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007995 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007996 vmx->nested.pi_desc_page = NULL;
7997 vmx->nested.pi_desc = NULL;
7998 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007999
Jim Mattsonde3a0022017-11-27 17:22:25 -06008000 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008001}
8002
8003/* Emulate the VMXOFF instruction */
8004static int handle_vmoff(struct kvm_vcpu *vcpu)
8005{
8006 if (!nested_vmx_check_permission(vcpu))
8007 return 1;
8008 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008009 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008010 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008011}
8012
Nadav Har'El27d6c862011-05-25 23:06:59 +03008013/* Emulate the VMCLEAR instruction */
8014static int handle_vmclear(struct kvm_vcpu *vcpu)
8015{
8016 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008017 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008018 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008019
8020 if (!nested_vmx_check_permission(vcpu))
8021 return 1;
8022
Radim Krčmářcbf71272017-05-19 15:48:51 +02008023 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008024 return 1;
8025
Radim Krčmářcbf71272017-05-19 15:48:51 +02008026 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8027 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8028 return kvm_skip_emulated_instruction(vcpu);
8029 }
8030
8031 if (vmptr == vmx->nested.vmxon_ptr) {
8032 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8033 return kvm_skip_emulated_instruction(vcpu);
8034 }
8035
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008036 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008037 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008038
Jim Mattson587d7e722017-03-02 12:41:48 -08008039 kvm_vcpu_write_guest(vcpu,
8040 vmptr + offsetof(struct vmcs12, launch_state),
8041 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008042
Nadav Har'El27d6c862011-05-25 23:06:59 +03008043 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008044 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008045}
8046
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008047static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8048
8049/* Emulate the VMLAUNCH instruction */
8050static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8051{
8052 return nested_vmx_run(vcpu, true);
8053}
8054
8055/* Emulate the VMRESUME instruction */
8056static int handle_vmresume(struct kvm_vcpu *vcpu)
8057{
8058
8059 return nested_vmx_run(vcpu, false);
8060}
8061
Nadav Har'El49f705c2011-05-25 23:08:30 +03008062/*
8063 * Read a vmcs12 field. Since these can have varying lengths and we return
8064 * one type, we chose the biggest type (u64) and zero-extend the return value
8065 * to that size. Note that the caller, handle_vmread, might need to use only
8066 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8067 * 64-bit fields are to be returned).
8068 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008069static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8070 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008071{
8072 short offset = vmcs_field_to_offset(field);
8073 char *p;
8074
8075 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008076 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008077
8078 p = ((char *)(get_vmcs12(vcpu))) + offset;
8079
Jim Mattsond37f4262017-12-22 12:12:16 -08008080 switch (vmcs_field_width(field)) {
8081 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008082 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008083 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008084 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008085 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008086 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008087 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008088 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008089 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008090 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008091 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008092 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008093 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008094 WARN_ON(1);
8095 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008096 }
8097}
8098
Abel Gordon20b97fe2013-04-18 14:36:25 +03008099
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008100static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8101 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008102 short offset = vmcs_field_to_offset(field);
8103 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8104 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008105 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008106
Jim Mattsond37f4262017-12-22 12:12:16 -08008107 switch (vmcs_field_width(field)) {
8108 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008109 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008110 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008111 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008112 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008113 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008114 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008115 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008116 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008117 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008118 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008119 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008120 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008121 WARN_ON(1);
8122 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008123 }
8124
8125}
8126
Abel Gordon16f5b902013-04-18 14:38:25 +03008127static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8128{
8129 int i;
8130 unsigned long field;
8131 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008132 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008133 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02008134 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03008135
Jan Kiszka282da872014-10-08 18:05:39 +02008136 preempt_disable();
8137
Abel Gordon16f5b902013-04-18 14:38:25 +03008138 vmcs_load(shadow_vmcs);
8139
8140 for (i = 0; i < num_fields; i++) {
8141 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008142 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03008143 vmcs12_write_any(&vmx->vcpu, field, field_value);
8144 }
8145
8146 vmcs_clear(shadow_vmcs);
8147 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008148
8149 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008150}
8151
Abel Gordonc3114422013-04-18 14:38:55 +03008152static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8153{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008154 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008155 shadow_read_write_fields,
8156 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008157 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008158 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008159 max_shadow_read_write_fields,
8160 max_shadow_read_only_fields
8161 };
8162 int i, q;
8163 unsigned long field;
8164 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008165 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008166
8167 vmcs_load(shadow_vmcs);
8168
Mathias Krausec2bae892013-06-26 20:36:21 +02008169 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008170 for (i = 0; i < max_fields[q]; i++) {
8171 field = fields[q][i];
8172 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008173 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008174 }
8175 }
8176
8177 vmcs_clear(shadow_vmcs);
8178 vmcs_load(vmx->loaded_vmcs->vmcs);
8179}
8180
Nadav Har'El49f705c2011-05-25 23:08:30 +03008181/*
8182 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8183 * used before) all generate the same failure when it is missing.
8184 */
8185static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8186{
8187 struct vcpu_vmx *vmx = to_vmx(vcpu);
8188 if (vmx->nested.current_vmptr == -1ull) {
8189 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008190 return 0;
8191 }
8192 return 1;
8193}
8194
8195static int handle_vmread(struct kvm_vcpu *vcpu)
8196{
8197 unsigned long field;
8198 u64 field_value;
8199 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8200 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8201 gva_t gva = 0;
8202
Kyle Hueyeb277562016-11-29 12:40:39 -08008203 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008204 return 1;
8205
Kyle Huey6affcbe2016-11-29 12:40:40 -08008206 if (!nested_vmx_check_vmcs12(vcpu))
8207 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008208
Nadav Har'El49f705c2011-05-25 23:08:30 +03008209 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008210 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008211 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008212 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008213 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008214 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008215 }
8216 /*
8217 * Now copy part of this value to register or memory, as requested.
8218 * Note that the number of bits actually copied is 32 or 64 depending
8219 * on the guest's mode (32 or 64 bit), not on the given field's length.
8220 */
8221 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008222 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008223 field_value);
8224 } else {
8225 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008226 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008227 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008228 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03008229 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
8230 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
8231 }
8232
8233 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008234 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008235}
8236
8237
8238static int handle_vmwrite(struct kvm_vcpu *vcpu)
8239{
8240 unsigned long field;
8241 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008242 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008243 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8244 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008245
Nadav Har'El49f705c2011-05-25 23:08:30 +03008246 /* The value to write might be 32 or 64 bits, depending on L1's long
8247 * mode, and eventually we need to write that into a field of several
8248 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008249 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008250 * bits into the vmcs12 field.
8251 */
8252 u64 field_value = 0;
8253 struct x86_exception e;
8254
Kyle Hueyeb277562016-11-29 12:40:39 -08008255 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008256 return 1;
8257
Kyle Huey6affcbe2016-11-29 12:40:40 -08008258 if (!nested_vmx_check_vmcs12(vcpu))
8259 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008260
Nadav Har'El49f705c2011-05-25 23:08:30 +03008261 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008262 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008263 (((vmx_instruction_info) >> 3) & 0xf));
8264 else {
8265 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008266 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008267 return 1;
8268 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03008269 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008270 kvm_inject_page_fault(vcpu, &e);
8271 return 1;
8272 }
8273 }
8274
8275
Nadav Amit27e6fb52014-06-18 17:19:26 +03008276 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008277 if (vmcs_field_readonly(field)) {
8278 nested_vmx_failValid(vcpu,
8279 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008280 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008281 }
8282
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008283 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008284 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008285 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008286 }
8287
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008288 switch (field) {
8289#define SHADOW_FIELD_RW(x) case x:
8290#include "vmx_shadow_fields.h"
8291 /*
8292 * The fields that can be updated by L1 without a vmexit are
8293 * always updated in the vmcs02, the others go down the slow
8294 * path of prepare_vmcs02.
8295 */
8296 break;
8297 default:
8298 vmx->nested.dirty_vmcs12 = true;
8299 break;
8300 }
8301
Nadav Har'El49f705c2011-05-25 23:08:30 +03008302 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008303 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008304}
8305
Jim Mattsona8bc2842016-11-30 12:03:44 -08008306static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8307{
8308 vmx->nested.current_vmptr = vmptr;
8309 if (enable_shadow_vmcs) {
8310 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8311 SECONDARY_EXEC_SHADOW_VMCS);
8312 vmcs_write64(VMCS_LINK_POINTER,
8313 __pa(vmx->vmcs01.shadow_vmcs));
8314 vmx->nested.sync_shadow_vmcs = true;
8315 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008316 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008317}
8318
Nadav Har'El63846662011-05-25 23:07:29 +03008319/* Emulate the VMPTRLD instruction */
8320static int handle_vmptrld(struct kvm_vcpu *vcpu)
8321{
8322 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008323 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008324
8325 if (!nested_vmx_check_permission(vcpu))
8326 return 1;
8327
Radim Krčmářcbf71272017-05-19 15:48:51 +02008328 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008329 return 1;
8330
Radim Krčmářcbf71272017-05-19 15:48:51 +02008331 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8332 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8333 return kvm_skip_emulated_instruction(vcpu);
8334 }
8335
8336 if (vmptr == vmx->nested.vmxon_ptr) {
8337 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8338 return kvm_skip_emulated_instruction(vcpu);
8339 }
8340
Nadav Har'El63846662011-05-25 23:07:29 +03008341 if (vmx->nested.current_vmptr != vmptr) {
8342 struct vmcs12 *new_vmcs12;
8343 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008344 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8345 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008346 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008347 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008348 }
8349 new_vmcs12 = kmap(page);
8350 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8351 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008352 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008353 nested_vmx_failValid(vcpu,
8354 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008355 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008356 }
Nadav Har'El63846662011-05-25 23:07:29 +03008357
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008358 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008359 /*
8360 * Load VMCS12 from guest memory since it is not already
8361 * cached.
8362 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008363 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8364 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008365 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008366
Jim Mattsona8bc2842016-11-30 12:03:44 -08008367 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008368 }
8369
8370 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008371 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008372}
8373
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008374/* Emulate the VMPTRST instruction */
8375static int handle_vmptrst(struct kvm_vcpu *vcpu)
8376{
8377 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8378 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8379 gva_t vmcs_gva;
8380 struct x86_exception e;
8381
8382 if (!nested_vmx_check_permission(vcpu))
8383 return 1;
8384
8385 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008386 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008387 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008388 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008389 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8390 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8391 sizeof(u64), &e)) {
8392 kvm_inject_page_fault(vcpu, &e);
8393 return 1;
8394 }
8395 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008396 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008397}
8398
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008399/* Emulate the INVEPT instruction */
8400static int handle_invept(struct kvm_vcpu *vcpu)
8401{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008402 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008403 u32 vmx_instruction_info, types;
8404 unsigned long type;
8405 gva_t gva;
8406 struct x86_exception e;
8407 struct {
8408 u64 eptp, gpa;
8409 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008410
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008411 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008412 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008413 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008414 kvm_queue_exception(vcpu, UD_VECTOR);
8415 return 1;
8416 }
8417
8418 if (!nested_vmx_check_permission(vcpu))
8419 return 1;
8420
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008421 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008422 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008423
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008424 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008425
Jim Mattson85c856b2016-10-26 08:38:38 -07008426 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008427 nested_vmx_failValid(vcpu,
8428 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008429 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008430 }
8431
8432 /* According to the Intel VMX instruction reference, the memory
8433 * operand is read even if it isn't needed (e.g., for type==global)
8434 */
8435 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008436 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008437 return 1;
8438 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8439 sizeof(operand), &e)) {
8440 kvm_inject_page_fault(vcpu, &e);
8441 return 1;
8442 }
8443
8444 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008445 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008446 /*
8447 * TODO: track mappings and invalidate
8448 * single context requests appropriately
8449 */
8450 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008451 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008452 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008453 nested_vmx_succeed(vcpu);
8454 break;
8455 default:
8456 BUG_ON(1);
8457 break;
8458 }
8459
Kyle Huey6affcbe2016-11-29 12:40:40 -08008460 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008461}
8462
Petr Matouseka642fc32014-09-23 20:22:30 +02008463static int handle_invvpid(struct kvm_vcpu *vcpu)
8464{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008465 struct vcpu_vmx *vmx = to_vmx(vcpu);
8466 u32 vmx_instruction_info;
8467 unsigned long type, types;
8468 gva_t gva;
8469 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008470 struct {
8471 u64 vpid;
8472 u64 gla;
8473 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008474
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008475 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008476 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008477 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008478 kvm_queue_exception(vcpu, UD_VECTOR);
8479 return 1;
8480 }
8481
8482 if (!nested_vmx_check_permission(vcpu))
8483 return 1;
8484
8485 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8486 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8487
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008488 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008489 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008490
Jim Mattson85c856b2016-10-26 08:38:38 -07008491 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008492 nested_vmx_failValid(vcpu,
8493 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008494 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008495 }
8496
8497 /* according to the intel vmx instruction reference, the memory
8498 * operand is read even if it isn't needed (e.g., for type==global)
8499 */
8500 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8501 vmx_instruction_info, false, &gva))
8502 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008503 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8504 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008505 kvm_inject_page_fault(vcpu, &e);
8506 return 1;
8507 }
Jim Mattson40352602017-06-28 09:37:37 -07008508 if (operand.vpid >> 16) {
8509 nested_vmx_failValid(vcpu,
8510 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8511 return kvm_skip_emulated_instruction(vcpu);
8512 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008513
8514 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008515 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008516 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008517 nested_vmx_failValid(vcpu,
8518 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8519 return kvm_skip_emulated_instruction(vcpu);
8520 }
8521 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008522 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008523 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008524 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008525 nested_vmx_failValid(vcpu,
8526 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008527 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008528 }
8529 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008530 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008531 break;
8532 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008533 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008534 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008535 }
8536
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008537 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008538 nested_vmx_succeed(vcpu);
8539
Kyle Huey6affcbe2016-11-29 12:40:40 -08008540 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008541}
8542
Kai Huang843e4332015-01-28 10:54:28 +08008543static int handle_pml_full(struct kvm_vcpu *vcpu)
8544{
8545 unsigned long exit_qualification;
8546
8547 trace_kvm_pml_full(vcpu->vcpu_id);
8548
8549 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8550
8551 /*
8552 * PML buffer FULL happened while executing iret from NMI,
8553 * "blocked by NMI" bit has to be set before next VM entry.
8554 */
8555 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008556 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008557 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8558 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8559 GUEST_INTR_STATE_NMI);
8560
8561 /*
8562 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8563 * here.., and there's no userspace involvement needed for PML.
8564 */
8565 return 1;
8566}
8567
Yunhong Jiang64672c92016-06-13 14:19:59 -07008568static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8569{
8570 kvm_lapic_expired_hv_timer(vcpu);
8571 return 1;
8572}
8573
Bandan Das41ab9372017-08-03 15:54:43 -04008574static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8575{
8576 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008577 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8578
8579 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008580 switch (address & VMX_EPTP_MT_MASK) {
8581 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008582 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008583 return false;
8584 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008585 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008586 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008587 return false;
8588 break;
8589 default:
8590 return false;
8591 }
8592
David Hildenbrandbb97a012017-08-10 23:15:28 +02008593 /* only 4 levels page-walk length are valid */
8594 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008595 return false;
8596
8597 /* Reserved bits should not be set */
8598 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8599 return false;
8600
8601 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008602 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008603 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008604 return false;
8605 }
8606
8607 return true;
8608}
8609
8610static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8611 struct vmcs12 *vmcs12)
8612{
8613 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8614 u64 address;
8615 bool accessed_dirty;
8616 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8617
8618 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8619 !nested_cpu_has_ept(vmcs12))
8620 return 1;
8621
8622 if (index >= VMFUNC_EPTP_ENTRIES)
8623 return 1;
8624
8625
8626 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8627 &address, index * 8, 8))
8628 return 1;
8629
David Hildenbrandbb97a012017-08-10 23:15:28 +02008630 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008631
8632 /*
8633 * If the (L2) guest does a vmfunc to the currently
8634 * active ept pointer, we don't have to do anything else
8635 */
8636 if (vmcs12->ept_pointer != address) {
8637 if (!valid_ept_address(vcpu, address))
8638 return 1;
8639
8640 kvm_mmu_unload(vcpu);
8641 mmu->ept_ad = accessed_dirty;
8642 mmu->base_role.ad_disabled = !accessed_dirty;
8643 vmcs12->ept_pointer = address;
8644 /*
8645 * TODO: Check what's the correct approach in case
8646 * mmu reload fails. Currently, we just let the next
8647 * reload potentially fail
8648 */
8649 kvm_mmu_reload(vcpu);
8650 }
8651
8652 return 0;
8653}
8654
Bandan Das2a499e42017-08-03 15:54:41 -04008655static int handle_vmfunc(struct kvm_vcpu *vcpu)
8656{
Bandan Das27c42a12017-08-03 15:54:42 -04008657 struct vcpu_vmx *vmx = to_vmx(vcpu);
8658 struct vmcs12 *vmcs12;
8659 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8660
8661 /*
8662 * VMFUNC is only supported for nested guests, but we always enable the
8663 * secondary control for simplicity; for non-nested mode, fake that we
8664 * didn't by injecting #UD.
8665 */
8666 if (!is_guest_mode(vcpu)) {
8667 kvm_queue_exception(vcpu, UD_VECTOR);
8668 return 1;
8669 }
8670
8671 vmcs12 = get_vmcs12(vcpu);
8672 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8673 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008674
8675 switch (function) {
8676 case 0:
8677 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8678 goto fail;
8679 break;
8680 default:
8681 goto fail;
8682 }
8683 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008684
8685fail:
8686 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8687 vmcs_read32(VM_EXIT_INTR_INFO),
8688 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008689 return 1;
8690}
8691
Nadav Har'El0140cae2011-05-25 23:06:28 +03008692/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008693 * The exit handlers return 1 if the exit was handled fully and guest execution
8694 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8695 * to be done to userspace and return 0.
8696 */
Mathias Krause772e0312012-08-30 01:30:19 +02008697static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008698 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8699 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008700 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008701 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008702 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008703 [EXIT_REASON_CR_ACCESS] = handle_cr,
8704 [EXIT_REASON_DR_ACCESS] = handle_dr,
8705 [EXIT_REASON_CPUID] = handle_cpuid,
8706 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8707 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8708 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8709 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008710 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008711 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008712 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008713 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008714 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008715 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008716 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008717 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008718 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008719 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008720 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008721 [EXIT_REASON_VMOFF] = handle_vmoff,
8722 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008723 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8724 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008725 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008726 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008727 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008728 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008729 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008730 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008731 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8732 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008733 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8734 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008735 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008736 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008737 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008738 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008739 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008740 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008741 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008742 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008743 [EXIT_REASON_XSAVES] = handle_xsaves,
8744 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008745 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008746 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008747 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008748};
8749
8750static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008751 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008752
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008753static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8754 struct vmcs12 *vmcs12)
8755{
8756 unsigned long exit_qualification;
8757 gpa_t bitmap, last_bitmap;
8758 unsigned int port;
8759 int size;
8760 u8 b;
8761
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008762 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008763 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008764
8765 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8766
8767 port = exit_qualification >> 16;
8768 size = (exit_qualification & 7) + 1;
8769
8770 last_bitmap = (gpa_t)-1;
8771 b = -1;
8772
8773 while (size > 0) {
8774 if (port < 0x8000)
8775 bitmap = vmcs12->io_bitmap_a;
8776 else if (port < 0x10000)
8777 bitmap = vmcs12->io_bitmap_b;
8778 else
Joe Perches1d804d02015-03-30 16:46:09 -07008779 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008780 bitmap += (port & 0x7fff) / 8;
8781
8782 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008783 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008784 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008785 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008786 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008787
8788 port++;
8789 size--;
8790 last_bitmap = bitmap;
8791 }
8792
Joe Perches1d804d02015-03-30 16:46:09 -07008793 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008794}
8795
Nadav Har'El644d7112011-05-25 23:12:35 +03008796/*
8797 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8798 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8799 * disinterest in the current event (read or write a specific MSR) by using an
8800 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8801 */
8802static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8803 struct vmcs12 *vmcs12, u32 exit_reason)
8804{
8805 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8806 gpa_t bitmap;
8807
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008808 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008809 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008810
8811 /*
8812 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8813 * for the four combinations of read/write and low/high MSR numbers.
8814 * First we need to figure out which of the four to use:
8815 */
8816 bitmap = vmcs12->msr_bitmap;
8817 if (exit_reason == EXIT_REASON_MSR_WRITE)
8818 bitmap += 2048;
8819 if (msr_index >= 0xc0000000) {
8820 msr_index -= 0xc0000000;
8821 bitmap += 1024;
8822 }
8823
8824 /* Then read the msr_index'th bit from this bitmap: */
8825 if (msr_index < 1024*8) {
8826 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008827 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008828 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008829 return 1 & (b >> (msr_index & 7));
8830 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008831 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008832}
8833
8834/*
8835 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8836 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8837 * intercept (via guest_host_mask etc.) the current event.
8838 */
8839static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8840 struct vmcs12 *vmcs12)
8841{
8842 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8843 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008844 int reg;
8845 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008846
8847 switch ((exit_qualification >> 4) & 3) {
8848 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008849 reg = (exit_qualification >> 8) & 15;
8850 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008851 switch (cr) {
8852 case 0:
8853 if (vmcs12->cr0_guest_host_mask &
8854 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008855 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008856 break;
8857 case 3:
8858 if ((vmcs12->cr3_target_count >= 1 &&
8859 vmcs12->cr3_target_value0 == val) ||
8860 (vmcs12->cr3_target_count >= 2 &&
8861 vmcs12->cr3_target_value1 == val) ||
8862 (vmcs12->cr3_target_count >= 3 &&
8863 vmcs12->cr3_target_value2 == val) ||
8864 (vmcs12->cr3_target_count >= 4 &&
8865 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008866 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008867 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008868 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008869 break;
8870 case 4:
8871 if (vmcs12->cr4_guest_host_mask &
8872 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008873 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008874 break;
8875 case 8:
8876 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008877 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008878 break;
8879 }
8880 break;
8881 case 2: /* clts */
8882 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8883 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008884 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008885 break;
8886 case 1: /* mov from cr */
8887 switch (cr) {
8888 case 3:
8889 if (vmcs12->cpu_based_vm_exec_control &
8890 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008891 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008892 break;
8893 case 8:
8894 if (vmcs12->cpu_based_vm_exec_control &
8895 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008896 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008897 break;
8898 }
8899 break;
8900 case 3: /* lmsw */
8901 /*
8902 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8903 * cr0. Other attempted changes are ignored, with no exit.
8904 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008905 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008906 if (vmcs12->cr0_guest_host_mask & 0xe &
8907 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008908 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008909 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8910 !(vmcs12->cr0_read_shadow & 0x1) &&
8911 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008912 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008913 break;
8914 }
Joe Perches1d804d02015-03-30 16:46:09 -07008915 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008916}
8917
8918/*
8919 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8920 * should handle it ourselves in L0 (and then continue L2). Only call this
8921 * when in is_guest_mode (L2).
8922 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008923static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008924{
Nadav Har'El644d7112011-05-25 23:12:35 +03008925 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8926 struct vcpu_vmx *vmx = to_vmx(vcpu);
8927 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8928
Jim Mattson4f350c62017-09-14 16:31:44 -07008929 if (vmx->nested.nested_run_pending)
8930 return false;
8931
8932 if (unlikely(vmx->fail)) {
8933 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8934 vmcs_read32(VM_INSTRUCTION_ERROR));
8935 return true;
8936 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008937
David Matlackc9f04402017-08-01 14:00:40 -07008938 /*
8939 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008940 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8941 * Page). The CPU may write to these pages via their host
8942 * physical address while L2 is running, bypassing any
8943 * address-translation-based dirty tracking (e.g. EPT write
8944 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008945 *
8946 * Mark them dirty on every exit from L2 to prevent them from
8947 * getting out of sync with dirty tracking.
8948 */
8949 nested_mark_vmcs12_pages_dirty(vcpu);
8950
Jim Mattson4f350c62017-09-14 16:31:44 -07008951 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8952 vmcs_readl(EXIT_QUALIFICATION),
8953 vmx->idt_vectoring_info,
8954 intr_info,
8955 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8956 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008957
8958 switch (exit_reason) {
8959 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008960 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008961 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008962 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008963 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008964 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008965 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008966 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008967 else if (is_debug(intr_info) &&
8968 vcpu->guest_debug &
8969 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8970 return false;
8971 else if (is_breakpoint(intr_info) &&
8972 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8973 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008974 return vmcs12->exception_bitmap &
8975 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8976 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008977 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008978 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008979 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008980 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008981 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008982 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008983 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008984 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008985 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008986 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008987 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008988 case EXIT_REASON_HLT:
8989 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8990 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008991 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008992 case EXIT_REASON_INVLPG:
8993 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8994 case EXIT_REASON_RDPMC:
8995 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008996 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008997 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008998 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008999 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009000 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009001 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9002 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9003 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9004 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9005 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9006 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009007 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009008 /*
9009 * VMX instructions trap unconditionally. This allows L1 to
9010 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9011 */
Joe Perches1d804d02015-03-30 16:46:09 -07009012 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009013 case EXIT_REASON_CR_ACCESS:
9014 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9015 case EXIT_REASON_DR_ACCESS:
9016 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9017 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009018 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009019 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9020 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009021 case EXIT_REASON_MSR_READ:
9022 case EXIT_REASON_MSR_WRITE:
9023 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9024 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009025 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009026 case EXIT_REASON_MWAIT_INSTRUCTION:
9027 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009028 case EXIT_REASON_MONITOR_TRAP_FLAG:
9029 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009030 case EXIT_REASON_MONITOR_INSTRUCTION:
9031 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9032 case EXIT_REASON_PAUSE_INSTRUCTION:
9033 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9034 nested_cpu_has2(vmcs12,
9035 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9036 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009037 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009038 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009039 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009040 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009041 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009042 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009043 /*
9044 * The controls for "virtualize APIC accesses," "APIC-
9045 * register virtualization," and "virtual-interrupt
9046 * delivery" only come from vmcs12.
9047 */
Joe Perches1d804d02015-03-30 16:46:09 -07009048 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009049 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009050 /*
9051 * L0 always deals with the EPT violation. If nested EPT is
9052 * used, and the nested mmu code discovers that the address is
9053 * missing in the guest EPT table (EPT12), the EPT violation
9054 * will be injected with nested_ept_inject_page_fault()
9055 */
Joe Perches1d804d02015-03-30 16:46:09 -07009056 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009057 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009058 /*
9059 * L2 never uses directly L1's EPT, but rather L0's own EPT
9060 * table (shadow on EPT) or a merged EPT table that L0 built
9061 * (EPT on EPT). So any problems with the structure of the
9062 * table is L0's fault.
9063 */
Joe Perches1d804d02015-03-30 16:46:09 -07009064 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009065 case EXIT_REASON_INVPCID:
9066 return
9067 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9068 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009069 case EXIT_REASON_WBINVD:
9070 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9071 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009072 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009073 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9074 /*
9075 * This should never happen, since it is not possible to
9076 * set XSS to a non-zero value---neither in L1 nor in L2.
9077 * If if it were, XSS would have to be checked against
9078 * the XSS exit bitmap in vmcs12.
9079 */
9080 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009081 case EXIT_REASON_PREEMPTION_TIMER:
9082 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009083 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009084 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009085 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009086 case EXIT_REASON_VMFUNC:
9087 /* VM functions are emulated through L2->L0 vmexits. */
9088 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009089 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009090 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009091 }
9092}
9093
Paolo Bonzini7313c692017-07-27 10:31:25 +02009094static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9095{
9096 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9097
9098 /*
9099 * At this point, the exit interruption info in exit_intr_info
9100 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9101 * we need to query the in-kernel LAPIC.
9102 */
9103 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9104 if ((exit_intr_info &
9105 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9106 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9107 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9108 vmcs12->vm_exit_intr_error_code =
9109 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9110 }
9111
9112 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9113 vmcs_readl(EXIT_QUALIFICATION));
9114 return 1;
9115}
9116
Avi Kivity586f9602010-11-18 13:09:54 +02009117static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9118{
9119 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9120 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9121}
9122
Kai Huanga3eaa862015-11-04 13:46:05 +08009123static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009124{
Kai Huanga3eaa862015-11-04 13:46:05 +08009125 if (vmx->pml_pg) {
9126 __free_page(vmx->pml_pg);
9127 vmx->pml_pg = NULL;
9128 }
Kai Huang843e4332015-01-28 10:54:28 +08009129}
9130
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009131static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009132{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009133 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009134 u64 *pml_buf;
9135 u16 pml_idx;
9136
9137 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9138
9139 /* Do nothing if PML buffer is empty */
9140 if (pml_idx == (PML_ENTITY_NUM - 1))
9141 return;
9142
9143 /* PML index always points to next available PML buffer entity */
9144 if (pml_idx >= PML_ENTITY_NUM)
9145 pml_idx = 0;
9146 else
9147 pml_idx++;
9148
9149 pml_buf = page_address(vmx->pml_pg);
9150 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9151 u64 gpa;
9152
9153 gpa = pml_buf[pml_idx];
9154 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009155 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009156 }
9157
9158 /* reset PML index */
9159 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9160}
9161
9162/*
9163 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9164 * Called before reporting dirty_bitmap to userspace.
9165 */
9166static void kvm_flush_pml_buffers(struct kvm *kvm)
9167{
9168 int i;
9169 struct kvm_vcpu *vcpu;
9170 /*
9171 * We only need to kick vcpu out of guest mode here, as PML buffer
9172 * is flushed at beginning of all VMEXITs, and it's obvious that only
9173 * vcpus running in guest are possible to have unflushed GPAs in PML
9174 * buffer.
9175 */
9176 kvm_for_each_vcpu(i, vcpu, kvm)
9177 kvm_vcpu_kick(vcpu);
9178}
9179
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009180static void vmx_dump_sel(char *name, uint32_t sel)
9181{
9182 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009183 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009184 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9185 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9186 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9187}
9188
9189static void vmx_dump_dtsel(char *name, uint32_t limit)
9190{
9191 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9192 name, vmcs_read32(limit),
9193 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9194}
9195
9196static void dump_vmcs(void)
9197{
9198 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9199 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9200 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9201 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9202 u32 secondary_exec_control = 0;
9203 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009204 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009205 int i, n;
9206
9207 if (cpu_has_secondary_exec_ctrls())
9208 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9209
9210 pr_err("*** Guest State ***\n");
9211 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9212 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9213 vmcs_readl(CR0_GUEST_HOST_MASK));
9214 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9215 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9216 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9217 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9218 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9219 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009220 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9221 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9222 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9223 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009224 }
9225 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9226 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9227 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9228 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9229 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9230 vmcs_readl(GUEST_SYSENTER_ESP),
9231 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9232 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9233 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9234 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9235 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9236 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9237 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9238 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9239 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9240 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9241 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9242 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9243 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009244 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9245 efer, vmcs_read64(GUEST_IA32_PAT));
9246 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9247 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009248 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009249 if (cpu_has_load_perf_global_ctrl &&
9250 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009251 pr_err("PerfGlobCtl = 0x%016llx\n",
9252 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009253 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009254 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009255 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9256 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9257 vmcs_read32(GUEST_ACTIVITY_STATE));
9258 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9259 pr_err("InterruptStatus = %04x\n",
9260 vmcs_read16(GUEST_INTR_STATUS));
9261
9262 pr_err("*** Host State ***\n");
9263 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9264 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9265 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9266 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9267 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9268 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9269 vmcs_read16(HOST_TR_SELECTOR));
9270 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9271 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9272 vmcs_readl(HOST_TR_BASE));
9273 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9274 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9275 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9276 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9277 vmcs_readl(HOST_CR4));
9278 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9279 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9280 vmcs_read32(HOST_IA32_SYSENTER_CS),
9281 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9282 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009283 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9284 vmcs_read64(HOST_IA32_EFER),
9285 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009286 if (cpu_has_load_perf_global_ctrl &&
9287 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009288 pr_err("PerfGlobCtl = 0x%016llx\n",
9289 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009290
9291 pr_err("*** Control State ***\n");
9292 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9293 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9294 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9295 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9296 vmcs_read32(EXCEPTION_BITMAP),
9297 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9298 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9299 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9300 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9301 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9302 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9303 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9304 vmcs_read32(VM_EXIT_INTR_INFO),
9305 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9306 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9307 pr_err(" reason=%08x qualification=%016lx\n",
9308 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9309 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9310 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9311 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009312 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009313 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009314 pr_err("TSC Multiplier = 0x%016llx\n",
9315 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009316 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9317 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9318 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9319 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9320 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009321 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009322 n = vmcs_read32(CR3_TARGET_COUNT);
9323 for (i = 0; i + 1 < n; i += 4)
9324 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9325 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9326 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9327 if (i < n)
9328 pr_err("CR3 target%u=%016lx\n",
9329 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9330 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9331 pr_err("PLE Gap=%08x Window=%08x\n",
9332 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9333 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9334 pr_err("Virtual processor ID = 0x%04x\n",
9335 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9336}
9337
Avi Kivity6aa8b732006-12-10 02:21:36 -08009338/*
9339 * The guest has exited. See if we can fix it or if we need userspace
9340 * assistance.
9341 */
Avi Kivity851ba692009-08-24 11:10:17 +03009342static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009343{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009344 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009345 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009346 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009347
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009348 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9349
Kai Huang843e4332015-01-28 10:54:28 +08009350 /*
9351 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9352 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9353 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9354 * mode as if vcpus is in root mode, the PML buffer must has been
9355 * flushed already.
9356 */
9357 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009358 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009359
Mohammed Gamal80ced182009-09-01 12:48:18 +02009360 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009361 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009362 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009363
Paolo Bonzini7313c692017-07-27 10:31:25 +02009364 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9365 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009366
Mohammed Gamal51207022010-05-31 22:40:54 +03009367 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009368 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009369 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9370 vcpu->run->fail_entry.hardware_entry_failure_reason
9371 = exit_reason;
9372 return 0;
9373 }
9374
Avi Kivity29bd8a72007-09-10 17:27:03 +03009375 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009376 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9377 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009378 = vmcs_read32(VM_INSTRUCTION_ERROR);
9379 return 0;
9380 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009381
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009382 /*
9383 * Note:
9384 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9385 * delivery event since it indicates guest is accessing MMIO.
9386 * The vm-exit can be triggered again after return to guest that
9387 * will cause infinite loop.
9388 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009389 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009390 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009391 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009392 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009393 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9394 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9395 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009396 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009397 vcpu->run->internal.data[0] = vectoring_info;
9398 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009399 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9400 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9401 vcpu->run->internal.ndata++;
9402 vcpu->run->internal.data[3] =
9403 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9404 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009405 return 0;
9406 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009407
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009408 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009409 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9410 if (vmx_interrupt_allowed(vcpu)) {
9411 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9412 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9413 vcpu->arch.nmi_pending) {
9414 /*
9415 * This CPU don't support us in finding the end of an
9416 * NMI-blocked window if the guest runs with IRQs
9417 * disabled. So we pull the trigger after 1 s of
9418 * futile waiting, but inform the user about this.
9419 */
9420 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9421 "state on VCPU %d after 1 s timeout\n",
9422 __func__, vcpu->vcpu_id);
9423 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9424 }
9425 }
9426
Avi Kivity6aa8b732006-12-10 02:21:36 -08009427 if (exit_reason < kvm_vmx_max_exit_handlers
9428 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009429 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009430 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009431 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9432 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009433 kvm_queue_exception(vcpu, UD_VECTOR);
9434 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009435 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009436}
9437
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009438static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009439{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009440 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9441
9442 if (is_guest_mode(vcpu) &&
9443 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9444 return;
9445
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009446 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009447 vmcs_write32(TPR_THRESHOLD, 0);
9448 return;
9449 }
9450
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009451 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009452}
9453
Jim Mattson8d860bb2018-05-09 16:56:05 -04009454static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009455{
9456 u32 sec_exec_control;
9457
Jim Mattson8d860bb2018-05-09 16:56:05 -04009458 if (!lapic_in_kernel(vcpu))
9459 return;
9460
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009461 /* Postpone execution until vmcs01 is the current VMCS. */
9462 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009463 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009464 return;
9465 }
9466
Paolo Bonzini35754c92015-07-29 12:05:37 +02009467 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009468 return;
9469
9470 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009471 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9472 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009473
Jim Mattson8d860bb2018-05-09 16:56:05 -04009474 switch (kvm_get_apic_mode(vcpu)) {
9475 case LAPIC_MODE_INVALID:
9476 WARN_ONCE(true, "Invalid local APIC state");
9477 case LAPIC_MODE_DISABLED:
9478 break;
9479 case LAPIC_MODE_XAPIC:
9480 if (flexpriority_enabled) {
9481 sec_exec_control |=
9482 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9483 vmx_flush_tlb(vcpu, true);
9484 }
9485 break;
9486 case LAPIC_MODE_X2APIC:
9487 if (cpu_has_vmx_virtualize_x2apic_mode())
9488 sec_exec_control |=
9489 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9490 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009491 }
9492 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9493
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009494 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009495}
9496
Tang Chen38b99172014-09-24 15:57:54 +08009497static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9498{
Jim Mattsonab5df312018-05-09 17:02:03 -04009499 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009500 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009501 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009502 }
Tang Chen38b99172014-09-24 15:57:54 +08009503}
9504
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009505static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009506{
9507 u16 status;
9508 u8 old;
9509
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009510 if (max_isr == -1)
9511 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009512
9513 status = vmcs_read16(GUEST_INTR_STATUS);
9514 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009515 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009516 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009517 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009518 vmcs_write16(GUEST_INTR_STATUS, status);
9519 }
9520}
9521
9522static void vmx_set_rvi(int vector)
9523{
9524 u16 status;
9525 u8 old;
9526
Wei Wang4114c272014-11-05 10:53:43 +08009527 if (vector == -1)
9528 vector = 0;
9529
Yang Zhangc7c9c562013-01-25 10:18:51 +08009530 status = vmcs_read16(GUEST_INTR_STATUS);
9531 old = (u8)status & 0xff;
9532 if ((u8)vector != old) {
9533 status &= ~0xff;
9534 status |= (u8)vector;
9535 vmcs_write16(GUEST_INTR_STATUS, status);
9536 }
9537}
9538
9539static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9540{
Liran Alon851c1a182017-12-24 18:12:56 +02009541 /*
9542 * When running L2, updating RVI is only relevant when
9543 * vmcs12 virtual-interrupt-delivery enabled.
9544 * However, it can be enabled only when L1 also
9545 * intercepts external-interrupts and in that case
9546 * we should not update vmcs02 RVI but instead intercept
9547 * interrupt. Therefore, do nothing when running L2.
9548 */
9549 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009550 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009551}
9552
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009553static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009554{
9555 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009556 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009557 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009558
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009559 WARN_ON(!vcpu->arch.apicv_active);
9560 if (pi_test_on(&vmx->pi_desc)) {
9561 pi_clear_on(&vmx->pi_desc);
9562 /*
9563 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9564 * But on x86 this is just a compiler barrier anyway.
9565 */
9566 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009567 max_irr_updated =
9568 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9569
9570 /*
9571 * If we are running L2 and L1 has a new pending interrupt
9572 * which can be injected, we should re-evaluate
9573 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009574 * If L1 intercepts external-interrupts, we should
9575 * exit from L2 to L1. Otherwise, interrupt should be
9576 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009577 */
Liran Alon851c1a182017-12-24 18:12:56 +02009578 if (is_guest_mode(vcpu) && max_irr_updated) {
9579 if (nested_exit_on_intr(vcpu))
9580 kvm_vcpu_exiting_guest_mode(vcpu);
9581 else
9582 kvm_make_request(KVM_REQ_EVENT, vcpu);
9583 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009584 } else {
9585 max_irr = kvm_lapic_find_highest_irr(vcpu);
9586 }
9587 vmx_hwapic_irr_update(vcpu, max_irr);
9588 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009589}
9590
Andrey Smetanin63086302015-11-10 15:36:32 +03009591static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009592{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009593 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009594 return;
9595
Yang Zhangc7c9c562013-01-25 10:18:51 +08009596 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9597 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9598 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9599 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9600}
9601
Paolo Bonzini967235d2016-12-19 14:03:45 +01009602static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9603{
9604 struct vcpu_vmx *vmx = to_vmx(vcpu);
9605
9606 pi_clear_on(&vmx->pi_desc);
9607 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9608}
9609
Avi Kivity51aa01d2010-07-20 14:31:20 +03009610static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009611{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009612 u32 exit_intr_info = 0;
9613 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009614
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009615 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9616 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009617 return;
9618
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009619 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9620 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9621 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009622
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009623 /* if exit due to PF check for async PF */
9624 if (is_page_fault(exit_intr_info))
9625 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9626
Andi Kleena0861c02009-06-08 17:37:09 +08009627 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009628 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9629 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009630 kvm_machine_check();
9631
Gleb Natapov20f65982009-05-11 13:35:55 +03009632 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009633 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009634 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009635 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009636 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009637 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009638}
Gleb Natapov20f65982009-05-11 13:35:55 +03009639
Yang Zhanga547c6d2013-04-11 19:25:10 +08009640static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9641{
9642 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9643
Yang Zhanga547c6d2013-04-11 19:25:10 +08009644 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9645 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9646 unsigned int vector;
9647 unsigned long entry;
9648 gate_desc *desc;
9649 struct vcpu_vmx *vmx = to_vmx(vcpu);
9650#ifdef CONFIG_X86_64
9651 unsigned long tmp;
9652#endif
9653
9654 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9655 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009656 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009657 asm volatile(
9658#ifdef CONFIG_X86_64
9659 "mov %%" _ASM_SP ", %[sp]\n\t"
9660 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9661 "push $%c[ss]\n\t"
9662 "push %[sp]\n\t"
9663#endif
9664 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009665 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009666 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009667 :
9668#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009669 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009670#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009671 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009672 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009673 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009674 [ss]"i"(__KERNEL_DS),
9675 [cs]"i"(__KERNEL_CS)
9676 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009677 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009678}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009679STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009680
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009681static bool vmx_has_high_real_mode_segbase(void)
9682{
9683 return enable_unrestricted_guest || emulate_invalid_guest_state;
9684}
9685
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009686static bool vmx_mpx_supported(void)
9687{
9688 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9689 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9690}
9691
Wanpeng Li55412b22014-12-02 19:21:30 +08009692static bool vmx_xsaves_supported(void)
9693{
9694 return vmcs_config.cpu_based_2nd_exec_ctrl &
9695 SECONDARY_EXEC_XSAVES;
9696}
9697
Avi Kivity51aa01d2010-07-20 14:31:20 +03009698static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9699{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009700 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009701 bool unblock_nmi;
9702 u8 vector;
9703 bool idtv_info_valid;
9704
9705 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009706
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009707 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009708 if (vmx->loaded_vmcs->nmi_known_unmasked)
9709 return;
9710 /*
9711 * Can't use vmx->exit_intr_info since we're not sure what
9712 * the exit reason is.
9713 */
9714 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9715 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9716 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9717 /*
9718 * SDM 3: 27.7.1.2 (September 2008)
9719 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9720 * a guest IRET fault.
9721 * SDM 3: 23.2.2 (September 2008)
9722 * Bit 12 is undefined in any of the following cases:
9723 * If the VM exit sets the valid bit in the IDT-vectoring
9724 * information field.
9725 * If the VM exit is due to a double fault.
9726 */
9727 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9728 vector != DF_VECTOR && !idtv_info_valid)
9729 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9730 GUEST_INTR_STATE_NMI);
9731 else
9732 vmx->loaded_vmcs->nmi_known_unmasked =
9733 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9734 & GUEST_INTR_STATE_NMI);
9735 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9736 vmx->loaded_vmcs->vnmi_blocked_time +=
9737 ktime_to_ns(ktime_sub(ktime_get(),
9738 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009739}
9740
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009741static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009742 u32 idt_vectoring_info,
9743 int instr_len_field,
9744 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009745{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009746 u8 vector;
9747 int type;
9748 bool idtv_info_valid;
9749
9750 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009751
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009752 vcpu->arch.nmi_injected = false;
9753 kvm_clear_exception_queue(vcpu);
9754 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009755
9756 if (!idtv_info_valid)
9757 return;
9758
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009759 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009760
Avi Kivity668f6122008-07-02 09:28:55 +03009761 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9762 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009763
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009764 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009765 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009766 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009767 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009768 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009769 * Clear bit "block by NMI" before VM entry if a NMI
9770 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009771 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009772 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009773 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009774 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009775 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009776 /* fall through */
9777 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009778 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009779 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009780 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009781 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009782 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009783 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009784 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009785 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009786 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009787 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009788 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009789 break;
9790 default:
9791 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009792 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009793}
9794
Avi Kivity83422e12010-07-20 14:43:23 +03009795static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9796{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009797 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009798 VM_EXIT_INSTRUCTION_LEN,
9799 IDT_VECTORING_ERROR_CODE);
9800}
9801
Avi Kivityb463a6f2010-07-20 15:06:17 +03009802static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9803{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009804 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009805 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9806 VM_ENTRY_INSTRUCTION_LEN,
9807 VM_ENTRY_EXCEPTION_ERROR_CODE);
9808
9809 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9810}
9811
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009812static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9813{
9814 int i, nr_msrs;
9815 struct perf_guest_switch_msr *msrs;
9816
9817 msrs = perf_guest_get_msrs(&nr_msrs);
9818
9819 if (!msrs)
9820 return;
9821
9822 for (i = 0; i < nr_msrs; i++)
9823 if (msrs[i].host == msrs[i].guest)
9824 clear_atomic_switch_msr(vmx, msrs[i].msr);
9825 else
9826 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9827 msrs[i].host);
9828}
9829
Jiang Biao33365e72016-11-03 15:03:37 +08009830static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009831{
9832 struct vcpu_vmx *vmx = to_vmx(vcpu);
9833 u64 tscl;
9834 u32 delta_tsc;
9835
9836 if (vmx->hv_deadline_tsc == -1)
9837 return;
9838
9839 tscl = rdtsc();
9840 if (vmx->hv_deadline_tsc > tscl)
9841 /* sure to be 32 bit only because checked on set_hv_timer */
9842 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9843 cpu_preemption_timer_multi);
9844 else
9845 delta_tsc = 0;
9846
9847 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9848}
9849
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009850static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009851{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009852 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009853 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009854
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009855 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009856 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009857 vmx->loaded_vmcs->soft_vnmi_blocked))
9858 vmx->loaded_vmcs->entry_time = ktime_get();
9859
Avi Kivity104f2262010-11-18 13:12:52 +02009860 /* Don't enter VMX if guest state is invalid, let the exit handler
9861 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009862 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009863 return;
9864
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009865 if (vmx->ple_window_dirty) {
9866 vmx->ple_window_dirty = false;
9867 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9868 }
9869
Abel Gordon012f83c2013-04-18 14:39:25 +03009870 if (vmx->nested.sync_shadow_vmcs) {
9871 copy_vmcs12_to_shadow(vmx);
9872 vmx->nested.sync_shadow_vmcs = false;
9873 }
9874
Avi Kivity104f2262010-11-18 13:12:52 +02009875 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9876 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9877 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9878 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9879
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009880 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009881 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009882 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009883 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009884 }
9885
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009886 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009887 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009888 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009889 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009890 }
9891
Avi Kivity104f2262010-11-18 13:12:52 +02009892 /* When single-stepping over STI and MOV SS, we must clear the
9893 * corresponding interruptibility bits in the guest state. Otherwise
9894 * vmentry fails as it then expects bit 14 (BS) in pending debug
9895 * exceptions being set, but that's not correct for the guest debugging
9896 * case. */
9897 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9898 vmx_set_interrupt_shadow(vcpu, 0);
9899
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009900 if (static_cpu_has(X86_FEATURE_PKU) &&
9901 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9902 vcpu->arch.pkru != vmx->host_pkru)
9903 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009904
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009905 atomic_switch_perf_msrs(vmx);
9906
Yunhong Jiang64672c92016-06-13 14:19:59 -07009907 vmx_arm_hv_timer(vcpu);
9908
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009909 /*
9910 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9911 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9912 * is no need to worry about the conditional branch over the wrmsr
9913 * being speculatively taken.
9914 */
9915 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009916 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009917
Nadav Har'Eld462b812011-05-24 15:26:10 +03009918 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009919
9920 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9921 (unsigned long)&current_evmcs->host_rsp : 0;
9922
Avi Kivity104f2262010-11-18 13:12:52 +02009923 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009924 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009925 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9926 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9927 "push %%" _ASM_CX " \n\t"
9928 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009929 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009930 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009931 /* Avoid VMWRITE when Enlightened VMCS is in use */
9932 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9933 "jz 2f \n\t"
9934 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9935 "jmp 1f \n\t"
9936 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009937 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009938 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009939 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009940 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9941 "mov %%cr2, %%" _ASM_DX " \n\t"
9942 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009943 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009944 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009945 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009946 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009947 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009948 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009949 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9950 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9951 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9952 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9953 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9954 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009955#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009956 "mov %c[r8](%0), %%r8 \n\t"
9957 "mov %c[r9](%0), %%r9 \n\t"
9958 "mov %c[r10](%0), %%r10 \n\t"
9959 "mov %c[r11](%0), %%r11 \n\t"
9960 "mov %c[r12](%0), %%r12 \n\t"
9961 "mov %c[r13](%0), %%r13 \n\t"
9962 "mov %c[r14](%0), %%r14 \n\t"
9963 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009964#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009965 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009966
Avi Kivity6aa8b732006-12-10 02:21:36 -08009967 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009968 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009969 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009970 "jmp 2f \n\t"
9971 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9972 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009973 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009974 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009975 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009976 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009977 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9978 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9979 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9980 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9981 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9982 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9983 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009984#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009985 "mov %%r8, %c[r8](%0) \n\t"
9986 "mov %%r9, %c[r9](%0) \n\t"
9987 "mov %%r10, %c[r10](%0) \n\t"
9988 "mov %%r11, %c[r11](%0) \n\t"
9989 "mov %%r12, %c[r12](%0) \n\t"
9990 "mov %%r13, %c[r13](%0) \n\t"
9991 "mov %%r14, %c[r14](%0) \n\t"
9992 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009993 "xor %%r8d, %%r8d \n\t"
9994 "xor %%r9d, %%r9d \n\t"
9995 "xor %%r10d, %%r10d \n\t"
9996 "xor %%r11d, %%r11d \n\t"
9997 "xor %%r12d, %%r12d \n\t"
9998 "xor %%r13d, %%r13d \n\t"
9999 "xor %%r14d, %%r14d \n\t"
10000 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010001#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010002 "mov %%cr2, %%" _ASM_AX " \n\t"
10003 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010004
Jim Mattson0cb5b302018-01-03 14:31:38 -080010005 "xor %%eax, %%eax \n\t"
10006 "xor %%ebx, %%ebx \n\t"
10007 "xor %%esi, %%esi \n\t"
10008 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010009 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010010 ".pushsection .rodata \n\t"
10011 ".global vmx_return \n\t"
10012 "vmx_return: " _ASM_PTR " 2b \n\t"
10013 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010014 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010015 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010016 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010017 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010018 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10019 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10020 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10021 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10022 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10023 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10024 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010025#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010026 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10027 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10028 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10029 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10030 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10031 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10032 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10033 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010034#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010035 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10036 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010037 : "cc", "memory"
10038#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010039 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010040 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010041#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010042 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010043#endif
10044 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010045
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010046 /*
10047 * We do not use IBRS in the kernel. If this vCPU has used the
10048 * SPEC_CTRL MSR it may have left it on; save the value and
10049 * turn it off. This is much more efficient than blindly adding
10050 * it to the atomic save/restore list. Especially as the former
10051 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10052 *
10053 * For non-nested case:
10054 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10055 * save it.
10056 *
10057 * For nested case:
10058 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10059 * save it.
10060 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010061 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010062 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010063
10064 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010065 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010066
David Woodhouse117cc7a2018-01-12 11:11:27 +000010067 /* Eliminate branch target predictions from guest mode */
10068 vmexit_fill_RSB();
10069
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010070 /* All fields are clean at this point */
10071 if (static_branch_unlikely(&enable_evmcs))
10072 current_evmcs->hv_clean_fields |=
10073 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10074
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010075 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010076 if (vmx->host_debugctlmsr)
10077 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010078
Avi Kivityaa67f602012-08-01 16:48:03 +030010079#ifndef CONFIG_X86_64
10080 /*
10081 * The sysexit path does not restore ds/es, so we must set them to
10082 * a reasonable value ourselves.
10083 *
10084 * We can't defer this to vmx_load_host_state() since that function
10085 * may be executed in interrupt context, which saves and restore segments
10086 * around it, nullifying its effect.
10087 */
10088 loadsegment(ds, __USER_DS);
10089 loadsegment(es, __USER_DS);
10090#endif
10091
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010092 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010093 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010094 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010095 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010096 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010097 vcpu->arch.regs_dirty = 0;
10098
Gleb Natapove0b890d2013-09-25 12:51:33 +030010099 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010100 * eager fpu is enabled if PKEY is supported and CR4 is switched
10101 * back on host, so it is safe to read guest PKRU from current
10102 * XSAVE.
10103 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010104 if (static_cpu_has(X86_FEATURE_PKU) &&
10105 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10106 vcpu->arch.pkru = __read_pkru();
10107 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010108 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010109 }
10110
Gleb Natapove0b890d2013-09-25 12:51:33 +030010111 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010112 vmx->idt_vectoring_info = 0;
10113
10114 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10115 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10116 return;
10117
10118 vmx->loaded_vmcs->launched = 1;
10119 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010120
Avi Kivity51aa01d2010-07-20 14:31:20 +030010121 vmx_complete_atomic_exit(vmx);
10122 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010123 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010124}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010125STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010126
Sean Christopherson434a1e92018-03-20 12:17:18 -070010127static struct kvm *vmx_vm_alloc(void)
10128{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010129 struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
10130 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010131}
10132
10133static void vmx_vm_free(struct kvm *kvm)
10134{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010135 kfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010136}
10137
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010138static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010139{
10140 struct vcpu_vmx *vmx = to_vmx(vcpu);
10141 int cpu;
10142
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010143 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010144 return;
10145
10146 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010147 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010148 vmx_vcpu_put(vcpu);
10149 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010150 put_cpu();
10151}
10152
Jim Mattson2f1fe812016-07-08 15:36:06 -070010153/*
10154 * Ensure that the current vmcs of the logical processor is the
10155 * vmcs01 of the vcpu before calling free_nested().
10156 */
10157static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10158{
10159 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010160
Christoffer Dallec7660c2017-12-04 21:35:23 +010010161 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010162 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010163 free_nested(vmx);
10164 vcpu_put(vcpu);
10165}
10166
Avi Kivity6aa8b732006-12-10 02:21:36 -080010167static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10168{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010169 struct vcpu_vmx *vmx = to_vmx(vcpu);
10170
Kai Huang843e4332015-01-28 10:54:28 +080010171 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010172 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010173 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010174 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010175 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010176 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010177 kfree(vmx->guest_msrs);
10178 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010179 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010180}
10181
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010182static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010183{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010184 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010185 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010186 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010187 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010188
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010189 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010190 return ERR_PTR(-ENOMEM);
10191
Wanpeng Li991e7a02015-09-16 17:30:05 +080010192 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010193
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010194 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10195 if (err)
10196 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010197
Peter Feiner4e595162016-07-07 14:49:58 -070010198 err = -ENOMEM;
10199
10200 /*
10201 * If PML is turned on, failure on enabling PML just results in failure
10202 * of creating the vcpu, therefore we can simplify PML logic (by
10203 * avoiding dealing with cases, such as enabling PML partially on vcpus
10204 * for the guest, etc.
10205 */
10206 if (enable_pml) {
10207 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10208 if (!vmx->pml_pg)
10209 goto uninit_vcpu;
10210 }
10211
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010212 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010213 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10214 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010215
Peter Feiner4e595162016-07-07 14:49:58 -070010216 if (!vmx->guest_msrs)
10217 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010218
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010219 err = alloc_loaded_vmcs(&vmx->vmcs01);
10220 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010221 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010222
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010223 msr_bitmap = vmx->vmcs01.msr_bitmap;
10224 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10225 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10226 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10227 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10228 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10229 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10230 vmx->msr_bitmap_mode = 0;
10231
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010232 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010233 cpu = get_cpu();
10234 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010235 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010236 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010237 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010238 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010239 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010240 err = alloc_apic_access_page(kvm);
10241 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010242 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010243 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010244
Sean Christophersone90008d2018-03-05 12:04:37 -080010245 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010246 err = init_rmode_identity_map(kvm);
10247 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010248 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010249 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010250
Wanpeng Li5c614b32015-10-13 09:18:36 -070010251 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010252 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10253 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010254 vmx->nested.vpid02 = allocate_vpid();
10255 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010256
Wincy Van705699a2015-02-03 23:58:17 +080010257 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010258 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010259
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010260 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10261
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010262 /*
10263 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10264 * or POSTED_INTR_WAKEUP_VECTOR.
10265 */
10266 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10267 vmx->pi_desc.sn = 1;
10268
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010269 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010270
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010271free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010272 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010273 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010274free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010275 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010276free_pml:
10277 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010278uninit_vcpu:
10279 kvm_vcpu_uninit(&vmx->vcpu);
10280free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010281 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010282 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010283 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010284}
10285
Wanpeng Lib31c1142018-03-12 04:53:04 -070010286static int vmx_vm_init(struct kvm *kvm)
10287{
10288 if (!ple_gap)
10289 kvm->arch.pause_in_guest = true;
10290 return 0;
10291}
10292
Yang, Sheng002c7f72007-07-31 14:23:01 +030010293static void __init vmx_check_processor_compat(void *rtn)
10294{
10295 struct vmcs_config vmcs_conf;
10296
10297 *(int *)rtn = 0;
10298 if (setup_vmcs_config(&vmcs_conf) < 0)
10299 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010300 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010301 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10302 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10303 smp_processor_id());
10304 *(int *)rtn = -EIO;
10305 }
10306}
10307
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010308static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010309{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010310 u8 cache;
10311 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010312
Sheng Yang522c68c2009-04-27 20:35:43 +080010313 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010314 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010315 * 2. EPT with VT-d:
10316 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010317 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010318 * b. VT-d with snooping control feature: snooping control feature of
10319 * VT-d engine can guarantee the cache correctness. Just set it
10320 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010321 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010322 * consistent with host MTRR
10323 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010324 if (is_mmio) {
10325 cache = MTRR_TYPE_UNCACHABLE;
10326 goto exit;
10327 }
10328
10329 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010330 ipat = VMX_EPT_IPAT_BIT;
10331 cache = MTRR_TYPE_WRBACK;
10332 goto exit;
10333 }
10334
10335 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10336 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010337 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010338 cache = MTRR_TYPE_WRBACK;
10339 else
10340 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010341 goto exit;
10342 }
10343
Xiao Guangrongff536042015-06-15 16:55:22 +080010344 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010345
10346exit:
10347 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010348}
10349
Sheng Yang17cc3932010-01-05 19:02:27 +080010350static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010351{
Sheng Yang878403b2010-01-05 19:02:29 +080010352 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10353 return PT_DIRECTORY_LEVEL;
10354 else
10355 /* For shadow and EPT supported 1GB page */
10356 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010357}
10358
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010359static void vmcs_set_secondary_exec_control(u32 new_ctl)
10360{
10361 /*
10362 * These bits in the secondary execution controls field
10363 * are dynamic, the others are mostly based on the hypervisor
10364 * architecture and the guest's CPUID. Do not touch the
10365 * dynamic bits.
10366 */
10367 u32 mask =
10368 SECONDARY_EXEC_SHADOW_VMCS |
10369 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010370 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10371 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010372
10373 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10374
10375 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10376 (new_ctl & ~mask) | (cur_ctl & mask));
10377}
10378
David Matlack8322ebb2016-11-29 18:14:09 -080010379/*
10380 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10381 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10382 */
10383static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10384{
10385 struct vcpu_vmx *vmx = to_vmx(vcpu);
10386 struct kvm_cpuid_entry2 *entry;
10387
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010388 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10389 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010390
10391#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10392 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010393 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010394} while (0)
10395
10396 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10397 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10398 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10399 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10400 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10401 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10402 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10403 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10404 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10405 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10406 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10407 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10408 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10409 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10410 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10411
10412 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10413 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10414 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10415 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10416 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010417 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010418
10419#undef cr4_fixed1_update
10420}
10421
Sheng Yang0e851882009-12-18 16:48:46 +080010422static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10423{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010424 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010425
Paolo Bonzini80154d72017-08-24 13:55:35 +020010426 if (cpu_has_secondary_exec_ctrls()) {
10427 vmx_compute_secondary_exec_control(vmx);
10428 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010429 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010430
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010431 if (nested_vmx_allowed(vcpu))
10432 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10433 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10434 else
10435 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10436 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010437
10438 if (nested_vmx_allowed(vcpu))
10439 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010440}
10441
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010442static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10443{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010444 if (func == 1 && nested)
10445 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010446}
10447
Yang Zhang25d92082013-08-06 12:00:32 +030010448static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10449 struct x86_exception *fault)
10450{
Jan Kiszka533558b2014-01-04 18:47:20 +010010451 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010452 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010453 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010454 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010455
Bandan Dasc5f983f2017-05-05 15:25:14 -040010456 if (vmx->nested.pml_full) {
10457 exit_reason = EXIT_REASON_PML_FULL;
10458 vmx->nested.pml_full = false;
10459 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10460 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010461 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010462 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010463 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010464
10465 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010466 vmcs12->guest_physical_address = fault->address;
10467}
10468
Peter Feiner995f00a2017-06-30 17:26:32 -070010469static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10470{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010471 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010472}
10473
Nadav Har'El155a97a2013-08-05 11:07:16 +030010474/* Callbacks for nested_ept_init_mmu_context: */
10475
10476static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10477{
10478 /* return the page table to be shadowed - in our case, EPT12 */
10479 return get_vmcs12(vcpu)->ept_pointer;
10480}
10481
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010482static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010483{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010484 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010485 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010486 return 1;
10487
10488 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010489 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010490 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010491 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010492 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010493 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10494 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10495 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10496
10497 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010498 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010499}
10500
10501static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10502{
10503 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10504}
10505
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010506static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10507 u16 error_code)
10508{
10509 bool inequality, bit;
10510
10511 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10512 inequality =
10513 (error_code & vmcs12->page_fault_error_code_mask) !=
10514 vmcs12->page_fault_error_code_match;
10515 return inequality ^ bit;
10516}
10517
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010518static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10519 struct x86_exception *fault)
10520{
10521 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10522
10523 WARN_ON(!is_guest_mode(vcpu));
10524
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010525 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10526 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010527 vmcs12->vm_exit_intr_error_code = fault->error_code;
10528 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10529 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10530 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10531 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010532 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010533 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010534 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010535}
10536
Paolo Bonzinic9923842017-12-13 14:16:30 +010010537static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10538 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010539
10540static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010541 struct vmcs12 *vmcs12)
10542{
10543 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010544 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010545 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010546
10547 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010548 /*
10549 * Translate L1 physical address to host physical
10550 * address for vmcs02. Keep the page pinned, so this
10551 * physical address remains valid. We keep a reference
10552 * to it so we can release it later.
10553 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010554 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010555 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010556 vmx->nested.apic_access_page = NULL;
10557 }
10558 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010559 /*
10560 * If translation failed, no matter: This feature asks
10561 * to exit when accessing the given address, and if it
10562 * can never be accessed, this feature won't do
10563 * anything anyway.
10564 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010565 if (!is_error_page(page)) {
10566 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010567 hpa = page_to_phys(vmx->nested.apic_access_page);
10568 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10569 } else {
10570 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10571 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10572 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010573 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010574
10575 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010576 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010577 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010578 vmx->nested.virtual_apic_page = NULL;
10579 }
10580 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010581
10582 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010583 * If translation failed, VM entry will fail because
10584 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10585 * Failing the vm entry is _not_ what the processor
10586 * does but it's basically the only possibility we
10587 * have. We could still enter the guest if CR8 load
10588 * exits are enabled, CR8 store exits are enabled, and
10589 * virtualize APIC access is disabled; in this case
10590 * the processor would never use the TPR shadow and we
10591 * could simply clear the bit from the execution
10592 * control. But such a configuration is useless, so
10593 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010594 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010595 if (!is_error_page(page)) {
10596 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010597 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10598 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10599 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010600 }
10601
Wincy Van705699a2015-02-03 23:58:17 +080010602 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010603 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10604 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010605 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010606 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010607 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010608 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10609 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010610 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010611 vmx->nested.pi_desc_page = page;
10612 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010613 vmx->nested.pi_desc =
10614 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10615 (unsigned long)(vmcs12->posted_intr_desc_addr &
10616 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010617 vmcs_write64(POSTED_INTR_DESC_ADDR,
10618 page_to_phys(vmx->nested.pi_desc_page) +
10619 (unsigned long)(vmcs12->posted_intr_desc_addr &
10620 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010621 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010622 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010623 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10624 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010625 else
10626 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10627 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010628}
10629
Jan Kiszkaf41245002014-03-07 20:03:13 +010010630static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10631{
10632 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10633 struct vcpu_vmx *vmx = to_vmx(vcpu);
10634
10635 if (vcpu->arch.virtual_tsc_khz == 0)
10636 return;
10637
10638 /* Make sure short timeouts reliably trigger an immediate vmexit.
10639 * hrtimer_start does not guarantee this. */
10640 if (preemption_timeout <= 1) {
10641 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10642 return;
10643 }
10644
10645 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10646 preemption_timeout *= 1000000;
10647 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10648 hrtimer_start(&vmx->nested.preemption_timer,
10649 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10650}
10651
Jim Mattson56a20512017-07-06 16:33:06 -070010652static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10653 struct vmcs12 *vmcs12)
10654{
10655 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10656 return 0;
10657
10658 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10659 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10660 return -EINVAL;
10661
10662 return 0;
10663}
10664
Wincy Van3af18d92015-02-03 23:49:31 +080010665static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10666 struct vmcs12 *vmcs12)
10667{
Wincy Van3af18d92015-02-03 23:49:31 +080010668 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10669 return 0;
10670
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010671 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010672 return -EINVAL;
10673
10674 return 0;
10675}
10676
Jim Mattson712b12d2017-08-24 13:24:47 -070010677static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10678 struct vmcs12 *vmcs12)
10679{
10680 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10681 return 0;
10682
10683 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10684 return -EINVAL;
10685
10686 return 0;
10687}
10688
Wincy Van3af18d92015-02-03 23:49:31 +080010689/*
10690 * Merge L0's and L1's MSR bitmap, return false to indicate that
10691 * we do not use the hardware.
10692 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010693static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10694 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010695{
Wincy Van82f0dd42015-02-03 23:57:18 +080010696 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010697 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010698 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010699 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010700 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010701 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010702 *
10703 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10704 * ensures that we do not accidentally generate an L02 MSR bitmap
10705 * from the L12 MSR bitmap that is too permissive.
10706 * 2. That L1 or L2s have actually used the MSR. This avoids
10707 * unnecessarily merging of the bitmap if the MSR is unused. This
10708 * works properly because we only update the L01 MSR bitmap lazily.
10709 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10710 * updated to reflect this when L1 (or its L2s) actually write to
10711 * the MSR.
10712 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010713 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10714 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010715
Paolo Bonzinic9923842017-12-13 14:16:30 +010010716 /* Nothing to do if the MSR bitmap is not in use. */
10717 if (!cpu_has_vmx_msr_bitmap() ||
10718 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10719 return false;
10720
Ashok Raj15d45072018-02-01 22:59:43 +010010721 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010722 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010723 return false;
10724
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010725 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10726 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010727 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010728
Radim Krčmářd048c092016-08-08 20:16:22 +020010729 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010730 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10731 /*
10732 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10733 * just lets the processor take the value from the virtual-APIC page;
10734 * take those 256 bits directly from the L1 bitmap.
10735 */
10736 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10737 unsigned word = msr / BITS_PER_LONG;
10738 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10739 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010740 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010741 } else {
10742 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10743 unsigned word = msr / BITS_PER_LONG;
10744 msr_bitmap_l0[word] = ~0;
10745 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10746 }
10747 }
10748
10749 nested_vmx_disable_intercept_for_msr(
10750 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010751 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010752 MSR_TYPE_W);
10753
10754 if (nested_cpu_has_vid(vmcs12)) {
10755 nested_vmx_disable_intercept_for_msr(
10756 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010757 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010758 MSR_TYPE_W);
10759 nested_vmx_disable_intercept_for_msr(
10760 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010761 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010762 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010763 }
Ashok Raj15d45072018-02-01 22:59:43 +010010764
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010765 if (spec_ctrl)
10766 nested_vmx_disable_intercept_for_msr(
10767 msr_bitmap_l1, msr_bitmap_l0,
10768 MSR_IA32_SPEC_CTRL,
10769 MSR_TYPE_R | MSR_TYPE_W);
10770
Ashok Raj15d45072018-02-01 22:59:43 +010010771 if (pred_cmd)
10772 nested_vmx_disable_intercept_for_msr(
10773 msr_bitmap_l1, msr_bitmap_l0,
10774 MSR_IA32_PRED_CMD,
10775 MSR_TYPE_W);
10776
Wincy Vanf2b93282015-02-03 23:56:03 +080010777 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010778 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010779
10780 return true;
10781}
10782
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040010783static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10784 struct vmcs12 *vmcs12)
10785{
10786 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10787 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10788 return -EINVAL;
10789 else
10790 return 0;
10791}
10792
Wincy Vanf2b93282015-02-03 23:56:03 +080010793static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10794 struct vmcs12 *vmcs12)
10795{
Wincy Van82f0dd42015-02-03 23:57:18 +080010796 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010797 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010798 !nested_cpu_has_vid(vmcs12) &&
10799 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010800 return 0;
10801
10802 /*
10803 * If virtualize x2apic mode is enabled,
10804 * virtualize apic access must be disabled.
10805 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010806 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10807 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010808 return -EINVAL;
10809
Wincy Van608406e2015-02-03 23:57:51 +080010810 /*
10811 * If virtual interrupt delivery is enabled,
10812 * we must exit on external interrupts.
10813 */
10814 if (nested_cpu_has_vid(vmcs12) &&
10815 !nested_exit_on_intr(vcpu))
10816 return -EINVAL;
10817
Wincy Van705699a2015-02-03 23:58:17 +080010818 /*
10819 * bits 15:8 should be zero in posted_intr_nv,
10820 * the descriptor address has been already checked
10821 * in nested_get_vmcs12_pages.
10822 */
10823 if (nested_cpu_has_posted_intr(vmcs12) &&
10824 (!nested_cpu_has_vid(vmcs12) ||
10825 !nested_exit_intr_ack_set(vcpu) ||
10826 vmcs12->posted_intr_nv & 0xff00))
10827 return -EINVAL;
10828
Wincy Vanf2b93282015-02-03 23:56:03 +080010829 /* tpr shadow is needed by all apicv features. */
10830 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10831 return -EINVAL;
10832
10833 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010834}
10835
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010836static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10837 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010838 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010839{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010840 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010841 u64 count, addr;
10842
10843 if (vmcs12_read_any(vcpu, count_field, &count) ||
10844 vmcs12_read_any(vcpu, addr_field, &addr)) {
10845 WARN_ON(1);
10846 return -EINVAL;
10847 }
10848 if (count == 0)
10849 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010850 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010851 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10852 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010853 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010854 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10855 addr_field, maxphyaddr, count, addr);
10856 return -EINVAL;
10857 }
10858 return 0;
10859}
10860
10861static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10862 struct vmcs12 *vmcs12)
10863{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010864 if (vmcs12->vm_exit_msr_load_count == 0 &&
10865 vmcs12->vm_exit_msr_store_count == 0 &&
10866 vmcs12->vm_entry_msr_load_count == 0)
10867 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010868 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010869 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010870 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010871 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010872 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010873 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010874 return -EINVAL;
10875 return 0;
10876}
10877
Bandan Dasc5f983f2017-05-05 15:25:14 -040010878static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10879 struct vmcs12 *vmcs12)
10880{
10881 u64 address = vmcs12->pml_address;
10882 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10883
10884 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10885 if (!nested_cpu_has_ept(vmcs12) ||
10886 !IS_ALIGNED(address, 4096) ||
10887 address >> maxphyaddr)
10888 return -EINVAL;
10889 }
10890
10891 return 0;
10892}
10893
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010894static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10895 struct vmx_msr_entry *e)
10896{
10897 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010898 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010899 return -EINVAL;
10900 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10901 e->index == MSR_IA32_UCODE_REV)
10902 return -EINVAL;
10903 if (e->reserved != 0)
10904 return -EINVAL;
10905 return 0;
10906}
10907
10908static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10909 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010910{
10911 if (e->index == MSR_FS_BASE ||
10912 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010913 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10914 nested_vmx_msr_check_common(vcpu, e))
10915 return -EINVAL;
10916 return 0;
10917}
10918
10919static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10920 struct vmx_msr_entry *e)
10921{
10922 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10923 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010924 return -EINVAL;
10925 return 0;
10926}
10927
10928/*
10929 * Load guest's/host's msr at nested entry/exit.
10930 * return 0 for success, entry index for failure.
10931 */
10932static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10933{
10934 u32 i;
10935 struct vmx_msr_entry e;
10936 struct msr_data msr;
10937
10938 msr.host_initiated = false;
10939 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010940 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10941 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010942 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010943 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10944 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010945 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010946 }
10947 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010948 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010949 "%s check failed (%u, 0x%x, 0x%x)\n",
10950 __func__, i, e.index, e.reserved);
10951 goto fail;
10952 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010953 msr.index = e.index;
10954 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010955 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010956 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010957 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10958 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010959 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010960 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010961 }
10962 return 0;
10963fail:
10964 return i + 1;
10965}
10966
10967static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10968{
10969 u32 i;
10970 struct vmx_msr_entry e;
10971
10972 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010973 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010974 if (kvm_vcpu_read_guest(vcpu,
10975 gpa + i * sizeof(e),
10976 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010977 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010978 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10979 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010980 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010981 }
10982 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010983 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010984 "%s check failed (%u, 0x%x, 0x%x)\n",
10985 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010986 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010987 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010988 msr_info.host_initiated = false;
10989 msr_info.index = e.index;
10990 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010991 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010992 "%s cannot read MSR (%u, 0x%x)\n",
10993 __func__, i, e.index);
10994 return -EINVAL;
10995 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010996 if (kvm_vcpu_write_guest(vcpu,
10997 gpa + i * sizeof(e) +
10998 offsetof(struct vmx_msr_entry, value),
10999 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011000 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011001 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011002 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011003 return -EINVAL;
11004 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011005 }
11006 return 0;
11007}
11008
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011009static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11010{
11011 unsigned long invalid_mask;
11012
11013 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11014 return (val & invalid_mask) == 0;
11015}
11016
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011017/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011018 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11019 * emulating VM entry into a guest with EPT enabled.
11020 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11021 * is assigned to entry_failure_code on failure.
11022 */
11023static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011024 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011025{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011026 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011027 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011028 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11029 return 1;
11030 }
11031
11032 /*
11033 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11034 * must not be dereferenced.
11035 */
11036 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11037 !nested_ept) {
11038 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11039 *entry_failure_code = ENTRY_FAIL_PDPTE;
11040 return 1;
11041 }
11042 }
11043
11044 vcpu->arch.cr3 = cr3;
11045 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11046 }
11047
11048 kvm_mmu_reset_context(vcpu);
11049 return 0;
11050}
11051
Jim Mattson6514dc32018-04-26 16:09:12 -070011052static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011053{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011054 struct vcpu_vmx *vmx = to_vmx(vcpu);
11055
11056 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11057 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11058 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11059 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11060 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11061 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11062 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11063 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11064 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11065 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11066 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11067 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11068 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11069 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11070 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11071 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11072 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11073 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11074 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11075 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11076 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11077 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11078 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11079 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11080 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11081 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11082 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11083 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11084 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11085 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11086 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011087
11088 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11089 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11090 vmcs12->guest_pending_dbg_exceptions);
11091 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11092 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11093
11094 if (nested_cpu_has_xsaves(vmcs12))
11095 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11096 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11097
11098 if (cpu_has_vmx_posted_intr())
11099 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11100
11101 /*
11102 * Whether page-faults are trapped is determined by a combination of
11103 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11104 * If enable_ept, L0 doesn't care about page faults and we should
11105 * set all of these to L1's desires. However, if !enable_ept, L0 does
11106 * care about (at least some) page faults, and because it is not easy
11107 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11108 * to exit on each and every L2 page fault. This is done by setting
11109 * MASK=MATCH=0 and (see below) EB.PF=1.
11110 * Note that below we don't need special code to set EB.PF beyond the
11111 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11112 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11113 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11114 */
11115 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11116 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11117 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11118 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11119
11120 /* All VMFUNCs are currently emulated through L0 vmexits. */
11121 if (cpu_has_vmx_vmfunc())
11122 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11123
11124 if (cpu_has_vmx_apicv()) {
11125 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11126 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11127 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11128 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11129 }
11130
11131 /*
11132 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11133 * Some constant fields are set here by vmx_set_constant_host_state().
11134 * Other fields are different per CPU, and will be set later when
11135 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11136 */
11137 vmx_set_constant_host_state(vmx);
11138
11139 /*
11140 * Set the MSR load/store lists to match L0's settings.
11141 */
11142 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11143 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11144 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11145 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11146 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11147
11148 set_cr4_guest_host_mask(vmx);
11149
11150 if (vmx_mpx_supported())
11151 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11152
11153 if (enable_vpid) {
11154 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11155 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11156 else
11157 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11158 }
11159
11160 /*
11161 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11162 */
11163 if (enable_ept) {
11164 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11165 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11166 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11167 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11168 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011169
11170 if (cpu_has_vmx_msr_bitmap())
11171 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011172}
11173
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011174/*
11175 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11176 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011177 * 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 +030011178 * guest in a way that will both be appropriate to L1's requests, and our
11179 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11180 * function also has additional necessary side-effects, like setting various
11181 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011182 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11183 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011184 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011185static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011186 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011187{
11188 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011189 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011190
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011191 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011192 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011193 vmx->nested.dirty_vmcs12 = false;
11194 }
11195
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011196 /*
11197 * First, the fields that are shadowed. This must be kept in sync
11198 * with vmx_shadow_fields.h.
11199 */
11200
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011201 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011202 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011203 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011204 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11205 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011206
11207 /*
11208 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11209 * HOST_FS_BASE, HOST_GS_BASE.
11210 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011211
Jim Mattson6514dc32018-04-26 16:09:12 -070011212 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011213 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011214 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11215 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11216 } else {
11217 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11218 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11219 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011220 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011221 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11222 vmcs12->vm_entry_intr_info_field);
11223 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11224 vmcs12->vm_entry_exception_error_code);
11225 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11226 vmcs12->vm_entry_instruction_len);
11227 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11228 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011229 vmx->loaded_vmcs->nmi_known_unmasked =
11230 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011231 } else {
11232 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11233 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011234 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011235
Jan Kiszkaf41245002014-03-07 20:03:13 +010011236 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011237
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011238 /* Preemption timer setting is only taken from vmcs01. */
11239 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11240 exec_control |= vmcs_config.pin_based_exec_ctrl;
11241 if (vmx->hv_deadline_tsc == -1)
11242 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11243
11244 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011245 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011246 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11247 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011248 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011249 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011250 }
Wincy Van705699a2015-02-03 23:58:17 +080011251
Jan Kiszkaf41245002014-03-07 20:03:13 +010011252 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011253
Jan Kiszkaf41245002014-03-07 20:03:13 +010011254 vmx->nested.preemption_timer_expired = false;
11255 if (nested_cpu_has_preemption_timer(vmcs12))
11256 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011257
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011258 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011259 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011260
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011261 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011262 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011263 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011264 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011265 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011266 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011267 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11268 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011269 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011270 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11271 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11272 ~SECONDARY_EXEC_ENABLE_PML;
11273 exec_control |= vmcs12_exec_ctrl;
11274 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011275
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011276 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011277 vmcs_write16(GUEST_INTR_STATUS,
11278 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011279
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011280 /*
11281 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11282 * nested_get_vmcs12_pages will either fix it up or
11283 * remove the VM execution control.
11284 */
11285 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11286 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11287
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011288 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11289 }
11290
Jim Mattson83bafef2016-10-04 10:48:38 -070011291 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011292 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11293 * entry, but only if the current (host) sp changed from the value
11294 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11295 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11296 * here we just force the write to happen on entry.
11297 */
11298 vmx->host_rsp = 0;
11299
11300 exec_control = vmx_exec_control(vmx); /* L0's desires */
11301 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11302 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11303 exec_control &= ~CPU_BASED_TPR_SHADOW;
11304 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011305
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011306 /*
11307 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11308 * nested_get_vmcs12_pages can't fix it up, the illegal value
11309 * will result in a VM entry failure.
11310 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011311 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011312 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011313 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011314 } else {
11315#ifdef CONFIG_X86_64
11316 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11317 CPU_BASED_CR8_STORE_EXITING;
11318#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011319 }
11320
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011321 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011322 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11323 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011324 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011325 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11326 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11327
11328 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11329
11330 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11331 * bitwise-or of what L1 wants to trap for L2, and what we want to
11332 * trap. Note that CR0.TS also needs updating - we do this later.
11333 */
11334 update_exception_bitmap(vcpu);
11335 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11336 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11337
Nadav Har'El8049d652013-08-05 11:07:06 +030011338 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11339 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11340 * bits are further modified by vmx_set_efer() below.
11341 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011342 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011343
11344 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11345 * emulated by vmx_set_efer(), below.
11346 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011347 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011348 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11349 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011350 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11351
Jim Mattson6514dc32018-04-26 16:09:12 -070011352 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011353 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011354 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011355 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011356 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011357 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011358 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011359
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011360 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11361
Peter Feinerc95ba922016-08-17 09:36:47 -070011362 if (kvm_has_tsc_control)
11363 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011364
11365 if (enable_vpid) {
11366 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011367 * There is no direct mapping between vpid02 and vpid12, the
11368 * vpid02 is per-vCPU for L0 and reused while the value of
11369 * vpid12 is changed w/ one invvpid during nested vmentry.
11370 * The vpid12 is allocated by L1 for L2, so it will not
11371 * influence global bitmap(for vpid01 and vpid02 allocation)
11372 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011373 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011374 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011375 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11376 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011377 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011378 }
11379 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011380 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011381 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011382 }
11383
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011384 if (enable_pml) {
11385 /*
11386 * Conceptually we want to copy the PML address and index from
11387 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11388 * since we always flush the log on each vmexit, this happens
11389 * to be equivalent to simply resetting the fields in vmcs02.
11390 */
11391 ASSERT(vmx->pml_pg);
11392 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11393 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11394 }
11395
Nadav Har'El155a97a2013-08-05 11:07:16 +030011396 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011397 if (nested_ept_init_mmu_context(vcpu)) {
11398 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11399 return 1;
11400 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011401 } else if (nested_cpu_has2(vmcs12,
11402 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011403 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011404 }
11405
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011406 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011407 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11408 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011409 * The CR0_READ_SHADOW is what L2 should have expected to read given
11410 * the specifications by L1; It's not enough to take
11411 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11412 * have more bits than L1 expected.
11413 */
11414 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11415 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11416
11417 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11418 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11419
Jim Mattson6514dc32018-04-26 16:09:12 -070011420 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011421 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011422 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11423 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11424 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11425 else
11426 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11427 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11428 vmx_set_efer(vcpu, vcpu->arch.efer);
11429
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011430 /*
11431 * Guest state is invalid and unrestricted guest is disabled,
11432 * which means L1 attempted VMEntry to L2 with invalid state.
11433 * Fail the VMEntry.
11434 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011435 if (vmx->emulation_required) {
11436 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011437 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011438 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011439
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011440 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011441 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011442 entry_failure_code))
11443 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011444
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011445 if (!enable_ept)
11446 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11447
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011448 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11449 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011450 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011451}
11452
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011453static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11454{
11455 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11456 nested_cpu_has_virtual_nmis(vmcs12))
11457 return -EINVAL;
11458
11459 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11460 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11461 return -EINVAL;
11462
11463 return 0;
11464}
11465
Jim Mattsonca0bde22016-11-30 12:03:46 -080011466static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11467{
11468 struct vcpu_vmx *vmx = to_vmx(vcpu);
11469
11470 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11471 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11472 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11473
Jim Mattson56a20512017-07-06 16:33:06 -070011474 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11475 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11476
Jim Mattsonca0bde22016-11-30 12:03:46 -080011477 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11478 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11479
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011480 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11481 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11482
Jim Mattson712b12d2017-08-24 13:24:47 -070011483 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11484 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11485
Jim Mattsonca0bde22016-11-30 12:03:46 -080011486 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11487 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11488
11489 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11490 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11491
Bandan Dasc5f983f2017-05-05 15:25:14 -040011492 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11493 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11494
Jim Mattsonca0bde22016-11-30 12:03:46 -080011495 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011496 vmx->nested.msrs.procbased_ctls_low,
11497 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011498 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11499 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011500 vmx->nested.msrs.secondary_ctls_low,
11501 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011502 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011503 vmx->nested.msrs.pinbased_ctls_low,
11504 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011505 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011506 vmx->nested.msrs.exit_ctls_low,
11507 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011508 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011509 vmx->nested.msrs.entry_ctls_low,
11510 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011511 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11512
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011513 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011514 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11515
Bandan Das41ab9372017-08-03 15:54:43 -040011516 if (nested_cpu_has_vmfunc(vmcs12)) {
11517 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011518 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011519 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11520
11521 if (nested_cpu_has_eptp_switching(vmcs12)) {
11522 if (!nested_cpu_has_ept(vmcs12) ||
11523 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11524 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11525 }
11526 }
Bandan Das27c42a12017-08-03 15:54:42 -040011527
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011528 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11529 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11530
Jim Mattsonca0bde22016-11-30 12:03:46 -080011531 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11532 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11533 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11534 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11535
11536 return 0;
11537}
11538
11539static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11540 u32 *exit_qual)
11541{
11542 bool ia32e;
11543
11544 *exit_qual = ENTRY_FAIL_DEFAULT;
11545
11546 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11547 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11548 return 1;
11549
11550 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11551 vmcs12->vmcs_link_pointer != -1ull) {
11552 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11553 return 1;
11554 }
11555
11556 /*
11557 * If the load IA32_EFER VM-entry control is 1, the following checks
11558 * are performed on the field for the IA32_EFER MSR:
11559 * - Bits reserved in the IA32_EFER MSR must be 0.
11560 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11561 * the IA-32e mode guest VM-exit control. It must also be identical
11562 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11563 * CR0.PG) is 1.
11564 */
11565 if (to_vmx(vcpu)->nested.nested_run_pending &&
11566 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11567 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11568 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11569 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11570 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11571 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11572 return 1;
11573 }
11574
11575 /*
11576 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11577 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11578 * the values of the LMA and LME bits in the field must each be that of
11579 * the host address-space size VM-exit control.
11580 */
11581 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11582 ia32e = (vmcs12->vm_exit_controls &
11583 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11584 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11585 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11586 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11587 return 1;
11588 }
11589
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011590 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11591 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11592 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11593 return 1;
11594
Jim Mattsonca0bde22016-11-30 12:03:46 -080011595 return 0;
11596}
11597
Jim Mattson6514dc32018-04-26 16:09:12 -070011598static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011599{
11600 struct vcpu_vmx *vmx = to_vmx(vcpu);
11601 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011602 u32 msr_entry_idx;
11603 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011604 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011605
Jim Mattson858e25c2016-11-30 12:03:47 -080011606 enter_guest_mode(vcpu);
11607
11608 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11609 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11610
Jim Mattsonde3a0022017-11-27 17:22:25 -060011611 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011612 vmx_segment_cache_clear(vmx);
11613
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011614 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11615 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11616
11617 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011618 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011619 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011620
11621 nested_get_vmcs12_pages(vcpu, vmcs12);
11622
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011623 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011624 msr_entry_idx = nested_vmx_load_msr(vcpu,
11625 vmcs12->vm_entry_msr_load_addr,
11626 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011627 if (msr_entry_idx)
11628 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011629
Jim Mattson858e25c2016-11-30 12:03:47 -080011630 /*
11631 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11632 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11633 * returned as far as L1 is concerned. It will only return (and set
11634 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11635 */
11636 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011637
11638fail:
11639 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11640 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11641 leave_guest_mode(vcpu);
11642 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11643 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11644 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011645}
11646
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011647/*
11648 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11649 * for running an L2 nested guest.
11650 */
11651static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11652{
11653 struct vmcs12 *vmcs12;
11654 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011655 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011656 u32 exit_qual;
11657 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011658
Kyle Hueyeb277562016-11-29 12:40:39 -080011659 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011660 return 1;
11661
Kyle Hueyeb277562016-11-29 12:40:39 -080011662 if (!nested_vmx_check_vmcs12(vcpu))
11663 goto out;
11664
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011665 vmcs12 = get_vmcs12(vcpu);
11666
Abel Gordon012f83c2013-04-18 14:39:25 +030011667 if (enable_shadow_vmcs)
11668 copy_shadow_to_vmcs12(vmx);
11669
Nadav Har'El7c177932011-05-25 23:12:04 +030011670 /*
11671 * The nested entry process starts with enforcing various prerequisites
11672 * on vmcs12 as required by the Intel SDM, and act appropriately when
11673 * they fail: As the SDM explains, some conditions should cause the
11674 * instruction to fail, while others will cause the instruction to seem
11675 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11676 * To speed up the normal (success) code path, we should avoid checking
11677 * for misconfigurations which will anyway be caught by the processor
11678 * when using the merged vmcs02.
11679 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011680 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11681 nested_vmx_failValid(vcpu,
11682 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11683 goto out;
11684 }
11685
Nadav Har'El7c177932011-05-25 23:12:04 +030011686 if (vmcs12->launch_state == launch) {
11687 nested_vmx_failValid(vcpu,
11688 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11689 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011690 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011691 }
11692
Jim Mattsonca0bde22016-11-30 12:03:46 -080011693 ret = check_vmentry_prereqs(vcpu, vmcs12);
11694 if (ret) {
11695 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011696 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011697 }
11698
Nadav Har'El7c177932011-05-25 23:12:04 +030011699 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011700 * After this point, the trap flag no longer triggers a singlestep trap
11701 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11702 * This is not 100% correct; for performance reasons, we delegate most
11703 * of the checks on host state to the processor. If those fail,
11704 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011705 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011706 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011707
Jim Mattsonca0bde22016-11-30 12:03:46 -080011708 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11709 if (ret) {
11710 nested_vmx_entry_failure(vcpu, vmcs12,
11711 EXIT_REASON_INVALID_STATE, exit_qual);
11712 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011713 }
11714
11715 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011716 * We're finally done with prerequisite checking, and can start with
11717 * the nested entry.
11718 */
11719
Jim Mattson6514dc32018-04-26 16:09:12 -070011720 vmx->nested.nested_run_pending = 1;
11721 ret = enter_vmx_non_root_mode(vcpu);
11722 if (ret) {
11723 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011724 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070011725 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011726
Chao Gao135a06c2018-02-11 10:06:30 +080011727 /*
11728 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11729 * by event injection, halt vcpu.
11730 */
11731 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011732 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11733 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011734 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011735 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011736 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011737
11738out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011739 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011740}
11741
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011742/*
11743 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11744 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11745 * This function returns the new value we should put in vmcs12.guest_cr0.
11746 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11747 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11748 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11749 * didn't trap the bit, because if L1 did, so would L0).
11750 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11751 * been modified by L2, and L1 knows it. So just leave the old value of
11752 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11753 * isn't relevant, because if L0 traps this bit it can set it to anything.
11754 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11755 * changed these bits, and therefore they need to be updated, but L0
11756 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11757 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11758 */
11759static inline unsigned long
11760vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11761{
11762 return
11763 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11764 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11765 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11766 vcpu->arch.cr0_guest_owned_bits));
11767}
11768
11769static inline unsigned long
11770vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11771{
11772 return
11773 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11774 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11775 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11776 vcpu->arch.cr4_guest_owned_bits));
11777}
11778
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011779static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11780 struct vmcs12 *vmcs12)
11781{
11782 u32 idt_vectoring;
11783 unsigned int nr;
11784
Wanpeng Li664f8e22017-08-24 03:35:09 -070011785 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011786 nr = vcpu->arch.exception.nr;
11787 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11788
11789 if (kvm_exception_is_soft(nr)) {
11790 vmcs12->vm_exit_instruction_len =
11791 vcpu->arch.event_exit_inst_len;
11792 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11793 } else
11794 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11795
11796 if (vcpu->arch.exception.has_error_code) {
11797 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11798 vmcs12->idt_vectoring_error_code =
11799 vcpu->arch.exception.error_code;
11800 }
11801
11802 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011803 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011804 vmcs12->idt_vectoring_info_field =
11805 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011806 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011807 nr = vcpu->arch.interrupt.nr;
11808 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11809
11810 if (vcpu->arch.interrupt.soft) {
11811 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11812 vmcs12->vm_entry_instruction_len =
11813 vcpu->arch.event_exit_inst_len;
11814 } else
11815 idt_vectoring |= INTR_TYPE_EXT_INTR;
11816
11817 vmcs12->idt_vectoring_info_field = idt_vectoring;
11818 }
11819}
11820
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011821static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11822{
11823 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011824 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011825 bool block_nested_events =
11826 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011827
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011828 if (vcpu->arch.exception.pending &&
11829 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011830 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011831 return -EBUSY;
11832 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011833 return 0;
11834 }
11835
Jan Kiszkaf41245002014-03-07 20:03:13 +010011836 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11837 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011838 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011839 return -EBUSY;
11840 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11841 return 0;
11842 }
11843
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011844 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011845 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011846 return -EBUSY;
11847 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11848 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11849 INTR_INFO_VALID_MASK, 0);
11850 /*
11851 * The NMI-triggered VM exit counts as injection:
11852 * clear this one and block further NMIs.
11853 */
11854 vcpu->arch.nmi_pending = 0;
11855 vmx_set_nmi_mask(vcpu, true);
11856 return 0;
11857 }
11858
11859 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11860 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011861 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011862 return -EBUSY;
11863 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011864 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011865 }
11866
David Hildenbrand6342c502017-01-25 11:58:58 +010011867 vmx_complete_nested_posted_interrupt(vcpu);
11868 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011869}
11870
Jan Kiszkaf41245002014-03-07 20:03:13 +010011871static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11872{
11873 ktime_t remaining =
11874 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11875 u64 value;
11876
11877 if (ktime_to_ns(remaining) <= 0)
11878 return 0;
11879
11880 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11881 do_div(value, 1000000);
11882 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11883}
11884
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011885/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011886 * Update the guest state fields of vmcs12 to reflect changes that
11887 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11888 * VM-entry controls is also updated, since this is really a guest
11889 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011890 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011891static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011892{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011893 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11894 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11895
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011896 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11897 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11898 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11899
11900 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11901 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11902 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11903 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11904 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11905 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11906 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11907 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11908 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11909 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11910 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11911 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11912 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11913 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11914 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11915 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11916 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11917 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11918 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11919 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11920 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11921 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11922 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11923 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11924 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11925 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11926 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11927 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11928 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11929 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11930 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11931 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11932 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11933 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11934 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11935 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11936
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011937 vmcs12->guest_interruptibility_info =
11938 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11939 vmcs12->guest_pending_dbg_exceptions =
11940 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011941 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11942 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11943 else
11944 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011945
Jan Kiszkaf41245002014-03-07 20:03:13 +010011946 if (nested_cpu_has_preemption_timer(vmcs12)) {
11947 if (vmcs12->vm_exit_controls &
11948 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11949 vmcs12->vmx_preemption_timer_value =
11950 vmx_get_preemption_timer_value(vcpu);
11951 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11952 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011953
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011954 /*
11955 * In some cases (usually, nested EPT), L2 is allowed to change its
11956 * own CR3 without exiting. If it has changed it, we must keep it.
11957 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11958 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11959 *
11960 * Additionally, restore L2's PDPTR to vmcs12.
11961 */
11962 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011963 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011964 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11965 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11966 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11967 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11968 }
11969
Jim Mattsond281e132017-06-01 12:44:46 -070011970 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011971
Wincy Van608406e2015-02-03 23:57:51 +080011972 if (nested_cpu_has_vid(vmcs12))
11973 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11974
Jan Kiszkac18911a2013-03-13 16:06:41 +010011975 vmcs12->vm_entry_controls =
11976 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011977 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011978
Jan Kiszka2996fca2014-06-16 13:59:43 +020011979 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11980 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11981 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11982 }
11983
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011984 /* TODO: These cannot have changed unless we have MSR bitmaps and
11985 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011986 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011987 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011988 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11989 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011990 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11991 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11992 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011993 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011994 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011995}
11996
11997/*
11998 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11999 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12000 * and this function updates it to reflect the changes to the guest state while
12001 * L2 was running (and perhaps made some exits which were handled directly by L0
12002 * without going back to L1), and to reflect the exit reason.
12003 * Note that we do not have to copy here all VMCS fields, just those that
12004 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12005 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12006 * which already writes to vmcs12 directly.
12007 */
12008static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12009 u32 exit_reason, u32 exit_intr_info,
12010 unsigned long exit_qualification)
12011{
12012 /* update guest state fields: */
12013 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012014
12015 /* update exit information fields: */
12016
Jan Kiszka533558b2014-01-04 18:47:20 +010012017 vmcs12->vm_exit_reason = exit_reason;
12018 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012019 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012020
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012021 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012022 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12023 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12024
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012025 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012026 vmcs12->launch_state = 1;
12027
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012028 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12029 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012030 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012031
12032 /*
12033 * Transfer the event that L0 or L1 may wanted to inject into
12034 * L2 to IDT_VECTORING_INFO_FIELD.
12035 */
12036 vmcs12_save_pending_event(vcpu, vmcs12);
12037 }
12038
12039 /*
12040 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12041 * preserved above and would only end up incorrectly in L1.
12042 */
12043 vcpu->arch.nmi_injected = false;
12044 kvm_clear_exception_queue(vcpu);
12045 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012046}
12047
Wanpeng Li5af41572017-11-05 16:54:49 -080012048static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12049 struct vmcs12 *vmcs12)
12050{
12051 u32 entry_failure_code;
12052
12053 nested_ept_uninit_mmu_context(vcpu);
12054
12055 /*
12056 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12057 * couldn't have changed.
12058 */
12059 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12060 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12061
12062 if (!enable_ept)
12063 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12064}
12065
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012066/*
12067 * A part of what we need to when the nested L2 guest exits and we want to
12068 * run its L1 parent, is to reset L1's guest state to the host state specified
12069 * in vmcs12.
12070 * This function is to be called not only on normal nested exit, but also on
12071 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12072 * Failures During or After Loading Guest State").
12073 * This function should be called when the active VMCS is L1's (vmcs01).
12074 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012075static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12076 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012077{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012078 struct kvm_segment seg;
12079
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012080 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12081 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012082 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012083 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12084 else
12085 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12086 vmx_set_efer(vcpu, vcpu->arch.efer);
12087
12088 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12089 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012090 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012091 /*
12092 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012093 * actually changed, because vmx_set_cr0 refers to efer set above.
12094 *
12095 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12096 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012097 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012098 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020012099 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012100
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012101 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012102 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012103 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012104
Wanpeng Li5af41572017-11-05 16:54:49 -080012105 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012106
Liran Alon6f1e03b2018-05-22 17:16:14 +030012107 /*
12108 * If vmcs01 don't use VPID, CPU flushes TLB on every
12109 * VMEntry/VMExit. Thus, no need to flush TLB.
12110 *
12111 * If vmcs12 uses VPID, TLB entries populated by L2 are
12112 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12113 * with vmx->vpid. Thus, no need to flush TLB.
12114 *
12115 * Therefore, flush TLB only in case vmcs01 uses VPID and
12116 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12117 * are both tagged with vmx->vpid.
12118 */
12119 if (enable_vpid &&
12120 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012121 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012122 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012123
12124 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12125 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12126 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12127 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12128 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012129 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12130 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012131
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012132 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12133 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12134 vmcs_write64(GUEST_BNDCFGS, 0);
12135
Jan Kiszka44811c02013-08-04 17:17:27 +020012136 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012137 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012138 vcpu->arch.pat = vmcs12->host_ia32_pat;
12139 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012140 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12141 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12142 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012143
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012144 /* Set L1 segment info according to Intel SDM
12145 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12146 seg = (struct kvm_segment) {
12147 .base = 0,
12148 .limit = 0xFFFFFFFF,
12149 .selector = vmcs12->host_cs_selector,
12150 .type = 11,
12151 .present = 1,
12152 .s = 1,
12153 .g = 1
12154 };
12155 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12156 seg.l = 1;
12157 else
12158 seg.db = 1;
12159 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12160 seg = (struct kvm_segment) {
12161 .base = 0,
12162 .limit = 0xFFFFFFFF,
12163 .type = 3,
12164 .present = 1,
12165 .s = 1,
12166 .db = 1,
12167 .g = 1
12168 };
12169 seg.selector = vmcs12->host_ds_selector;
12170 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12171 seg.selector = vmcs12->host_es_selector;
12172 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12173 seg.selector = vmcs12->host_ss_selector;
12174 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12175 seg.selector = vmcs12->host_fs_selector;
12176 seg.base = vmcs12->host_fs_base;
12177 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12178 seg.selector = vmcs12->host_gs_selector;
12179 seg.base = vmcs12->host_gs_base;
12180 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12181 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012182 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012183 .limit = 0x67,
12184 .selector = vmcs12->host_tr_selector,
12185 .type = 11,
12186 .present = 1
12187 };
12188 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12189
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012190 kvm_set_dr(vcpu, 7, 0x400);
12191 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012192
Wincy Van3af18d92015-02-03 23:49:31 +080012193 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012194 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012195
Wincy Vanff651cb2014-12-11 08:52:58 +030012196 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12197 vmcs12->vm_exit_msr_load_count))
12198 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012199}
12200
12201/*
12202 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12203 * and modify vmcs12 to make it see what it would expect to see there if
12204 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12205 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012206static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12207 u32 exit_intr_info,
12208 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012209{
12210 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012211 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12212
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012213 /* trying to cancel vmlaunch/vmresume is a bug */
12214 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12215
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012216 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012217 * The only expected VM-instruction error is "VM entry with
12218 * invalid control field(s)." Anything else indicates a
12219 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012220 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012221 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12222 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12223
12224 leave_guest_mode(vcpu);
12225
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012226 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12227 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12228
Jim Mattson4f350c62017-09-14 16:31:44 -070012229 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012230 if (exit_reason == -1)
12231 sync_vmcs12(vcpu, vmcs12);
12232 else
12233 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12234 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012235
12236 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12237 vmcs12->vm_exit_msr_store_count))
12238 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012239 }
12240
Jim Mattson4f350c62017-09-14 16:31:44 -070012241 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012242 vm_entry_controls_reset_shadow(vmx);
12243 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012244 vmx_segment_cache_clear(vmx);
12245
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012246 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012247 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12248 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012249 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012250 if (vmx->hv_deadline_tsc == -1)
12251 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12252 PIN_BASED_VMX_PREEMPTION_TIMER);
12253 else
12254 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12255 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012256 if (kvm_has_tsc_control)
12257 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012258
Jim Mattson8d860bb2018-05-09 16:56:05 -040012259 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12260 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12261 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012262 } else if (!nested_cpu_has_ept(vmcs12) &&
12263 nested_cpu_has2(vmcs12,
12264 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012265 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012266 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012267
12268 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12269 vmx->host_rsp = 0;
12270
12271 /* Unpin physical memory we referred to in vmcs02 */
12272 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012273 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012274 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012275 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012276 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012277 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012278 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012279 }
Wincy Van705699a2015-02-03 23:58:17 +080012280 if (vmx->nested.pi_desc_page) {
12281 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012282 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012283 vmx->nested.pi_desc_page = NULL;
12284 vmx->nested.pi_desc = NULL;
12285 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012286
12287 /*
Tang Chen38b99172014-09-24 15:57:54 +080012288 * We are now running in L2, mmu_notifier will force to reload the
12289 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12290 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012291 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012292
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012293 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012294 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012295
12296 /* in case we halted in L2 */
12297 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012298
12299 if (likely(!vmx->fail)) {
12300 /*
12301 * TODO: SDM says that with acknowledge interrupt on
12302 * exit, bit 31 of the VM-exit interrupt information
12303 * (valid interrupt) is always set to 1 on
12304 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12305 * need kvm_cpu_has_interrupt(). See the commit
12306 * message for details.
12307 */
12308 if (nested_exit_intr_ack_set(vcpu) &&
12309 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12310 kvm_cpu_has_interrupt(vcpu)) {
12311 int irq = kvm_cpu_get_interrupt(vcpu);
12312 WARN_ON(irq < 0);
12313 vmcs12->vm_exit_intr_info = irq |
12314 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12315 }
12316
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012317 if (exit_reason != -1)
12318 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12319 vmcs12->exit_qualification,
12320 vmcs12->idt_vectoring_info_field,
12321 vmcs12->vm_exit_intr_info,
12322 vmcs12->vm_exit_intr_error_code,
12323 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012324
12325 load_vmcs12_host_state(vcpu, vmcs12);
12326
12327 return;
12328 }
12329
12330 /*
12331 * After an early L2 VM-entry failure, we're now back
12332 * in L1 which thinks it just finished a VMLAUNCH or
12333 * VMRESUME instruction, so we need to set the failure
12334 * flag and the VM-instruction error field of the VMCS
12335 * accordingly.
12336 */
12337 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012338
12339 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12340
Jim Mattson4f350c62017-09-14 16:31:44 -070012341 /*
12342 * The emulated instruction was already skipped in
12343 * nested_vmx_run, but the updated RIP was never
12344 * written back to the vmcs01.
12345 */
12346 skip_emulated_instruction(vcpu);
12347 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012348}
12349
Nadav Har'El7c177932011-05-25 23:12:04 +030012350/*
Jan Kiszka42124922014-01-04 18:47:19 +010012351 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12352 */
12353static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12354{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012355 if (is_guest_mode(vcpu)) {
12356 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012357 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012358 }
Jan Kiszka42124922014-01-04 18:47:19 +010012359 free_nested(to_vmx(vcpu));
12360}
12361
12362/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012363 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12364 * 23.7 "VM-entry failures during or after loading guest state" (this also
12365 * lists the acceptable exit-reason and exit-qualification parameters).
12366 * It should only be called before L2 actually succeeded to run, and when
12367 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12368 */
12369static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12370 struct vmcs12 *vmcs12,
12371 u32 reason, unsigned long qualification)
12372{
12373 load_vmcs12_host_state(vcpu, vmcs12);
12374 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12375 vmcs12->exit_qualification = qualification;
12376 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012377 if (enable_shadow_vmcs)
12378 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012379}
12380
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012381static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12382 struct x86_instruction_info *info,
12383 enum x86_intercept_stage stage)
12384{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012385 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12386 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12387
12388 /*
12389 * RDPID causes #UD if disabled through secondary execution controls.
12390 * Because it is marked as EmulateOnUD, we need to intercept it here.
12391 */
12392 if (info->intercept == x86_intercept_rdtscp &&
12393 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12394 ctxt->exception.vector = UD_VECTOR;
12395 ctxt->exception.error_code_valid = false;
12396 return X86EMUL_PROPAGATE_FAULT;
12397 }
12398
12399 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012400 return X86EMUL_CONTINUE;
12401}
12402
Yunhong Jiang64672c92016-06-13 14:19:59 -070012403#ifdef CONFIG_X86_64
12404/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12405static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12406 u64 divisor, u64 *result)
12407{
12408 u64 low = a << shift, high = a >> (64 - shift);
12409
12410 /* To avoid the overflow on divq */
12411 if (high >= divisor)
12412 return 1;
12413
12414 /* Low hold the result, high hold rem which is discarded */
12415 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12416 "rm" (divisor), "0" (low), "1" (high));
12417 *result = low;
12418
12419 return 0;
12420}
12421
12422static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12423{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012424 struct vcpu_vmx *vmx;
12425 u64 tscl, guest_tscl, delta_tsc;
12426
12427 if (kvm_mwait_in_guest(vcpu->kvm))
12428 return -EOPNOTSUPP;
12429
12430 vmx = to_vmx(vcpu);
12431 tscl = rdtsc();
12432 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12433 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012434
12435 /* Convert to host delta tsc if tsc scaling is enabled */
12436 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12437 u64_shl_div_u64(delta_tsc,
12438 kvm_tsc_scaling_ratio_frac_bits,
12439 vcpu->arch.tsc_scaling_ratio,
12440 &delta_tsc))
12441 return -ERANGE;
12442
12443 /*
12444 * If the delta tsc can't fit in the 32 bit after the multi shift,
12445 * we can't use the preemption timer.
12446 * It's possible that it fits on later vmentries, but checking
12447 * on every vmentry is costly so we just use an hrtimer.
12448 */
12449 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12450 return -ERANGE;
12451
12452 vmx->hv_deadline_tsc = tscl + delta_tsc;
12453 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12454 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012455
12456 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012457}
12458
12459static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12460{
12461 struct vcpu_vmx *vmx = to_vmx(vcpu);
12462 vmx->hv_deadline_tsc = -1;
12463 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12464 PIN_BASED_VMX_PREEMPTION_TIMER);
12465}
12466#endif
12467
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012468static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012469{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012470 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012471 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012472}
12473
Kai Huang843e4332015-01-28 10:54:28 +080012474static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12475 struct kvm_memory_slot *slot)
12476{
12477 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12478 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12479}
12480
12481static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12482 struct kvm_memory_slot *slot)
12483{
12484 kvm_mmu_slot_set_dirty(kvm, slot);
12485}
12486
12487static void vmx_flush_log_dirty(struct kvm *kvm)
12488{
12489 kvm_flush_pml_buffers(kvm);
12490}
12491
Bandan Dasc5f983f2017-05-05 15:25:14 -040012492static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12493{
12494 struct vmcs12 *vmcs12;
12495 struct vcpu_vmx *vmx = to_vmx(vcpu);
12496 gpa_t gpa;
12497 struct page *page = NULL;
12498 u64 *pml_address;
12499
12500 if (is_guest_mode(vcpu)) {
12501 WARN_ON_ONCE(vmx->nested.pml_full);
12502
12503 /*
12504 * Check if PML is enabled for the nested guest.
12505 * Whether eptp bit 6 is set is already checked
12506 * as part of A/D emulation.
12507 */
12508 vmcs12 = get_vmcs12(vcpu);
12509 if (!nested_cpu_has_pml(vmcs12))
12510 return 0;
12511
Dan Carpenter47698862017-05-10 22:43:17 +030012512 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012513 vmx->nested.pml_full = true;
12514 return 1;
12515 }
12516
12517 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12518
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012519 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12520 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012521 return 0;
12522
12523 pml_address = kmap(page);
12524 pml_address[vmcs12->guest_pml_index--] = gpa;
12525 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012526 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012527 }
12528
12529 return 0;
12530}
12531
Kai Huang843e4332015-01-28 10:54:28 +080012532static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12533 struct kvm_memory_slot *memslot,
12534 gfn_t offset, unsigned long mask)
12535{
12536 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12537}
12538
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012539static void __pi_post_block(struct kvm_vcpu *vcpu)
12540{
12541 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12542 struct pi_desc old, new;
12543 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012544
12545 do {
12546 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012547 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12548 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012549
12550 dest = cpu_physical_id(vcpu->cpu);
12551
12552 if (x2apic_enabled())
12553 new.ndst = dest;
12554 else
12555 new.ndst = (dest << 8) & 0xFF00;
12556
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012557 /* set 'NV' to 'notification vector' */
12558 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012559 } while (cmpxchg64(&pi_desc->control, old.control,
12560 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012561
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012562 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12563 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012564 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012565 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012566 vcpu->pre_pcpu = -1;
12567 }
12568}
12569
Feng Wuefc64402015-09-18 22:29:51 +080012570/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012571 * This routine does the following things for vCPU which is going
12572 * to be blocked if VT-d PI is enabled.
12573 * - Store the vCPU to the wakeup list, so when interrupts happen
12574 * we can find the right vCPU to wake up.
12575 * - Change the Posted-interrupt descriptor as below:
12576 * 'NDST' <-- vcpu->pre_pcpu
12577 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12578 * - If 'ON' is set during this process, which means at least one
12579 * interrupt is posted for this vCPU, we cannot block it, in
12580 * this case, return 1, otherwise, return 0.
12581 *
12582 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012583static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012584{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012585 unsigned int dest;
12586 struct pi_desc old, new;
12587 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12588
12589 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012590 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12591 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012592 return 0;
12593
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012594 WARN_ON(irqs_disabled());
12595 local_irq_disable();
12596 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12597 vcpu->pre_pcpu = vcpu->cpu;
12598 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12599 list_add_tail(&vcpu->blocked_vcpu_list,
12600 &per_cpu(blocked_vcpu_on_cpu,
12601 vcpu->pre_pcpu));
12602 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12603 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012604
12605 do {
12606 old.control = new.control = pi_desc->control;
12607
Feng Wubf9f6ac2015-09-18 22:29:55 +080012608 WARN((pi_desc->sn == 1),
12609 "Warning: SN field of posted-interrupts "
12610 "is set before blocking\n");
12611
12612 /*
12613 * Since vCPU can be preempted during this process,
12614 * vcpu->cpu could be different with pre_pcpu, we
12615 * need to set pre_pcpu as the destination of wakeup
12616 * notification event, then we can find the right vCPU
12617 * to wakeup in wakeup handler if interrupts happen
12618 * when the vCPU is in blocked state.
12619 */
12620 dest = cpu_physical_id(vcpu->pre_pcpu);
12621
12622 if (x2apic_enabled())
12623 new.ndst = dest;
12624 else
12625 new.ndst = (dest << 8) & 0xFF00;
12626
12627 /* set 'NV' to 'wakeup vector' */
12628 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012629 } while (cmpxchg64(&pi_desc->control, old.control,
12630 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012631
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012632 /* We should not block the vCPU if an interrupt is posted for it. */
12633 if (pi_test_on(pi_desc) == 1)
12634 __pi_post_block(vcpu);
12635
12636 local_irq_enable();
12637 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012638}
12639
Yunhong Jiangbc225122016-06-13 14:19:58 -070012640static int vmx_pre_block(struct kvm_vcpu *vcpu)
12641{
12642 if (pi_pre_block(vcpu))
12643 return 1;
12644
Yunhong Jiang64672c92016-06-13 14:19:59 -070012645 if (kvm_lapic_hv_timer_in_use(vcpu))
12646 kvm_lapic_switch_to_sw_timer(vcpu);
12647
Yunhong Jiangbc225122016-06-13 14:19:58 -070012648 return 0;
12649}
12650
12651static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012652{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012653 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012654 return;
12655
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012656 WARN_ON(irqs_disabled());
12657 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012658 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012659 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012660}
12661
Yunhong Jiangbc225122016-06-13 14:19:58 -070012662static void vmx_post_block(struct kvm_vcpu *vcpu)
12663{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012664 if (kvm_x86_ops->set_hv_timer)
12665 kvm_lapic_switch_to_hv_timer(vcpu);
12666
Yunhong Jiangbc225122016-06-13 14:19:58 -070012667 pi_post_block(vcpu);
12668}
12669
Feng Wubf9f6ac2015-09-18 22:29:55 +080012670/*
Feng Wuefc64402015-09-18 22:29:51 +080012671 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12672 *
12673 * @kvm: kvm
12674 * @host_irq: host irq of the interrupt
12675 * @guest_irq: gsi of the interrupt
12676 * @set: set or unset PI
12677 * returns 0 on success, < 0 on failure
12678 */
12679static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12680 uint32_t guest_irq, bool set)
12681{
12682 struct kvm_kernel_irq_routing_entry *e;
12683 struct kvm_irq_routing_table *irq_rt;
12684 struct kvm_lapic_irq irq;
12685 struct kvm_vcpu *vcpu;
12686 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012687 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012688
12689 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012690 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12691 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012692 return 0;
12693
12694 idx = srcu_read_lock(&kvm->irq_srcu);
12695 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012696 if (guest_irq >= irq_rt->nr_rt_entries ||
12697 hlist_empty(&irq_rt->map[guest_irq])) {
12698 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12699 guest_irq, irq_rt->nr_rt_entries);
12700 goto out;
12701 }
Feng Wuefc64402015-09-18 22:29:51 +080012702
12703 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12704 if (e->type != KVM_IRQ_ROUTING_MSI)
12705 continue;
12706 /*
12707 * VT-d PI cannot support posting multicast/broadcast
12708 * interrupts to a vCPU, we still use interrupt remapping
12709 * for these kind of interrupts.
12710 *
12711 * For lowest-priority interrupts, we only support
12712 * those with single CPU as the destination, e.g. user
12713 * configures the interrupts via /proc/irq or uses
12714 * irqbalance to make the interrupts single-CPU.
12715 *
12716 * We will support full lowest-priority interrupt later.
12717 */
12718
Radim Krčmář371313132016-07-12 22:09:27 +020012719 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012720 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12721 /*
12722 * Make sure the IRTE is in remapped mode if
12723 * we don't handle it in posted mode.
12724 */
12725 ret = irq_set_vcpu_affinity(host_irq, NULL);
12726 if (ret < 0) {
12727 printk(KERN_INFO
12728 "failed to back to remapped mode, irq: %u\n",
12729 host_irq);
12730 goto out;
12731 }
12732
Feng Wuefc64402015-09-18 22:29:51 +080012733 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012734 }
Feng Wuefc64402015-09-18 22:29:51 +080012735
12736 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12737 vcpu_info.vector = irq.vector;
12738
hu huajun2698d822018-04-11 15:16:40 +080012739 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012740 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12741
12742 if (set)
12743 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012744 else
Feng Wuefc64402015-09-18 22:29:51 +080012745 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012746
12747 if (ret < 0) {
12748 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12749 __func__);
12750 goto out;
12751 }
12752 }
12753
12754 ret = 0;
12755out:
12756 srcu_read_unlock(&kvm->irq_srcu, idx);
12757 return ret;
12758}
12759
Ashok Rajc45dcc72016-06-22 14:59:56 +080012760static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12761{
12762 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12763 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12764 FEATURE_CONTROL_LMCE;
12765 else
12766 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12767 ~FEATURE_CONTROL_LMCE;
12768}
12769
Ladi Prosek72d7b372017-10-11 16:54:41 +020012770static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12771{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012772 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12773 if (to_vmx(vcpu)->nested.nested_run_pending)
12774 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012775 return 1;
12776}
12777
Ladi Prosek0234bf82017-10-11 16:54:40 +020012778static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12779{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012780 struct vcpu_vmx *vmx = to_vmx(vcpu);
12781
12782 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12783 if (vmx->nested.smm.guest_mode)
12784 nested_vmx_vmexit(vcpu, -1, 0, 0);
12785
12786 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12787 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012788 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012789 return 0;
12790}
12791
12792static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12793{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012794 struct vcpu_vmx *vmx = to_vmx(vcpu);
12795 int ret;
12796
12797 if (vmx->nested.smm.vmxon) {
12798 vmx->nested.vmxon = true;
12799 vmx->nested.smm.vmxon = false;
12800 }
12801
12802 if (vmx->nested.smm.guest_mode) {
12803 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070012804 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012805 vcpu->arch.hflags |= HF_SMM_MASK;
12806 if (ret)
12807 return ret;
12808
12809 vmx->nested.smm.guest_mode = false;
12810 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012811 return 0;
12812}
12813
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012814static int enable_smi_window(struct kvm_vcpu *vcpu)
12815{
12816 return 0;
12817}
12818
Kees Cook404f6aa2016-08-08 16:29:06 -070012819static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012820 .cpu_has_kvm_support = cpu_has_kvm_support,
12821 .disabled_by_bios = vmx_disabled_by_bios,
12822 .hardware_setup = hardware_setup,
12823 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012824 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012825 .hardware_enable = hardware_enable,
12826 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012827 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012828 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012829
Wanpeng Lib31c1142018-03-12 04:53:04 -070012830 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070012831 .vm_alloc = vmx_vm_alloc,
12832 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070012833
Avi Kivity6aa8b732006-12-10 02:21:36 -080012834 .vcpu_create = vmx_create_vcpu,
12835 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012836 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012837
Avi Kivity04d2cc72007-09-10 18:10:54 +030012838 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012839 .vcpu_load = vmx_vcpu_load,
12840 .vcpu_put = vmx_vcpu_put,
12841
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012842 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012843 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012844 .get_msr = vmx_get_msr,
12845 .set_msr = vmx_set_msr,
12846 .get_segment_base = vmx_get_segment_base,
12847 .get_segment = vmx_get_segment,
12848 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012849 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012850 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012851 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012852 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012853 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012854 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012855 .set_cr3 = vmx_set_cr3,
12856 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012857 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012858 .get_idt = vmx_get_idt,
12859 .set_idt = vmx_set_idt,
12860 .get_gdt = vmx_get_gdt,
12861 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012862 .get_dr6 = vmx_get_dr6,
12863 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012864 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012865 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012866 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012867 .get_rflags = vmx_get_rflags,
12868 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012869
Avi Kivity6aa8b732006-12-10 02:21:36 -080012870 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012871
Avi Kivity6aa8b732006-12-10 02:21:36 -080012872 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012873 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012874 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012875 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12876 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012877 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012878 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012879 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012880 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012881 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012882 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012883 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012884 .get_nmi_mask = vmx_get_nmi_mask,
12885 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012886 .enable_nmi_window = enable_nmi_window,
12887 .enable_irq_window = enable_irq_window,
12888 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040012889 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012890 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012891 .get_enable_apicv = vmx_get_enable_apicv,
12892 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012893 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012894 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012895 .hwapic_irr_update = vmx_hwapic_irr_update,
12896 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012897 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12898 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012899
Izik Eiduscbc94022007-10-25 00:29:55 +020012900 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070012901 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012902 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012903 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012904
Avi Kivity586f9602010-11-18 13:09:54 +020012905 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012906
Sheng Yang17cc3932010-01-05 19:02:27 +080012907 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012908
12909 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012910
12911 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012912 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012913
12914 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012915
12916 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012917
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012918 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012919 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012920
12921 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012922
12923 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012924 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012925 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012926 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012927 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012928
12929 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012930
12931 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012932
12933 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12934 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12935 .flush_log_dirty = vmx_flush_log_dirty,
12936 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012937 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012938
Feng Wubf9f6ac2015-09-18 22:29:55 +080012939 .pre_block = vmx_pre_block,
12940 .post_block = vmx_post_block,
12941
Wei Huang25462f72015-06-19 15:45:05 +020012942 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012943
12944 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012945
12946#ifdef CONFIG_X86_64
12947 .set_hv_timer = vmx_set_hv_timer,
12948 .cancel_hv_timer = vmx_cancel_hv_timer,
12949#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012950
12951 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012952
Ladi Prosek72d7b372017-10-11 16:54:41 +020012953 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012954 .pre_enter_smm = vmx_pre_enter_smm,
12955 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012956 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012957};
12958
12959static int __init vmx_init(void)
12960{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012961 int r;
12962
12963#if IS_ENABLED(CONFIG_HYPERV)
12964 /*
12965 * Enlightened VMCS usage should be recommended and the host needs
12966 * to support eVMCS v1 or above. We can also disable eVMCS support
12967 * with module parameter.
12968 */
12969 if (enlightened_vmcs &&
12970 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
12971 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
12972 KVM_EVMCS_VERSION) {
12973 int cpu;
12974
12975 /* Check that we have assist pages on all online CPUs */
12976 for_each_online_cpu(cpu) {
12977 if (!hv_get_vp_assist_page(cpu)) {
12978 enlightened_vmcs = false;
12979 break;
12980 }
12981 }
12982
12983 if (enlightened_vmcs) {
12984 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
12985 static_branch_enable(&enable_evmcs);
12986 }
12987 } else {
12988 enlightened_vmcs = false;
12989 }
12990#endif
12991
12992 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012993 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012994 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012995 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012996
Dave Young2965faa2015-09-09 15:38:55 -070012997#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012998 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12999 crash_vmclear_local_loaded_vmcss);
13000#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013001 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013002
He, Qingfdef3ad2007-04-30 09:45:24 +030013003 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013004}
13005
13006static void __exit vmx_exit(void)
13007{
Dave Young2965faa2015-09-09 15:38:55 -070013008#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013009 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013010 synchronize_rcu();
13011#endif
13012
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013013 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013014
13015#if IS_ENABLED(CONFIG_HYPERV)
13016 if (static_branch_unlikely(&enable_evmcs)) {
13017 int cpu;
13018 struct hv_vp_assist_page *vp_ap;
13019 /*
13020 * Reset everything to support using non-enlightened VMCS
13021 * access later (e.g. when we reload the module with
13022 * enlightened_vmcs=0)
13023 */
13024 for_each_online_cpu(cpu) {
13025 vp_ap = hv_get_vp_assist_page(cpu);
13026
13027 if (!vp_ap)
13028 continue;
13029
13030 vp_ap->current_nested_vmcs = 0;
13031 vp_ap->enlighten_vmentry = 0;
13032 }
13033
13034 static_branch_disable(&enable_evmcs);
13035 }
13036#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013037}
13038
13039module_init(vmx_init)
13040module_exit(vmx_exit)