blob: 39a83a66a28226e05783598143790d41ead9d6cf [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>
Paolo Bonziniecb586b2018-02-22 16:43:17 +010054#include <asm/microcode.h>
David Woodhouse117cc7a2018-01-12 11:11:27 +000055#include <asm/nospec-branch.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
124#define MSR_BITMAP_MODE_LM 4
125
Haozhong Zhang64903d62015-10-20 15:39:09 +0800126#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
127
Yunhong Jiang64672c92016-06-13 14:19:59 -0700128/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
129static int __read_mostly cpu_preemption_timer_multi;
130static bool __read_mostly enable_preemption_timer = 1;
131#ifdef CONFIG_X86_64
132module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
133#endif
134
Gleb Natapov50378782013-02-04 16:00:28 +0200135#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800136#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
137#define KVM_VM_CR0_ALWAYS_ON \
138 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
139 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200140#define KVM_CR4_GUEST_OWNED_BITS \
141 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800142 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200143
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800144#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200145#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
146#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
147
Avi Kivity78ac8b42010-04-08 18:19:35 +0300148#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
149
Jan Kiszkaf41245002014-03-07 20:03:13 +0100150#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
151
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800152/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300153 * Hyper-V requires all of these, so mark them as supported even though
154 * they are just treated the same as all-context.
155 */
156#define VMX_VPID_EXTENT_SUPPORTED_MASK \
157 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
158 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
160 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
161
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800162/*
163 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
164 * ple_gap: upper bound on the amount of time between two successive
165 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500166 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800167 * ple_window: upper bound on the amount of time a guest is allowed to execute
168 * in a PAUSE loop. Tests indicate that most spinlocks are held for
169 * less than 2^12 cycles
170 * Time is measured based on a counter that runs at the same rate as the TSC,
171 * refer SDM volume 3b section 21.6.13 & 22.1.3.
172 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400173static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800174
Babu Moger7fbc85a2018-03-16 16:37:22 -0400175static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
176module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800177
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200178/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400179static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400180module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200181
182/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400183static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400184module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200185
186/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400187static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200189
Avi Kivity83287ea422012-09-16 15:10:57 +0300190extern const ulong vmx_return;
191
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700192struct kvm_vmx {
193 struct kvm kvm;
194
195 unsigned int tss_addr;
196 bool ept_identity_pagetable_done;
197 gpa_t ept_identity_map_addr;
198};
199
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200200#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300201
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400202struct vmcs {
203 u32 revision_id;
204 u32 abort;
205 char data[0];
206};
207
Nadav Har'Eld462b812011-05-24 15:26:10 +0300208/*
209 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
210 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
211 * loaded on this CPU (so we can clear them if the CPU goes down).
212 */
213struct loaded_vmcs {
214 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700215 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300216 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200217 bool launched;
218 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200219 unsigned long vmcs_host_cr3; /* May not match real cr3 */
220 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100221 /* Support for vnmi-less CPUs */
222 int soft_vnmi_blocked;
223 ktime_t entry_time;
224 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100225 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300226 struct list_head loaded_vmcss_on_cpu_link;
227};
228
Avi Kivity26bb0982009-09-07 11:14:12 +0300229struct shared_msr_entry {
230 unsigned index;
231 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200232 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300233};
234
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300235/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300236 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
237 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
238 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
239 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
240 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
241 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600242 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300243 * underlying hardware which will be used to run L2.
244 * This structure is packed to ensure that its layout is identical across
245 * machines (necessary for live migration).
246 * If there are changes in this struct, VMCS12_REVISION must be changed.
247 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300248typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300249struct __packed vmcs12 {
250 /* According to the Intel spec, a VMCS region must start with the
251 * following two fields. Then follow implementation-specific data.
252 */
253 u32 revision_id;
254 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300255
Nadav Har'El27d6c862011-05-25 23:06:59 +0300256 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
257 u32 padding[7]; /* room for future expansion */
258
Nadav Har'El22bd0352011-05-25 23:05:57 +0300259 u64 io_bitmap_a;
260 u64 io_bitmap_b;
261 u64 msr_bitmap;
262 u64 vm_exit_msr_store_addr;
263 u64 vm_exit_msr_load_addr;
264 u64 vm_entry_msr_load_addr;
265 u64 tsc_offset;
266 u64 virtual_apic_page_addr;
267 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800268 u64 posted_intr_desc_addr;
Bandan Das27c42a12017-08-03 15:54:42 -0400269 u64 vm_function_control;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300270 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800271 u64 eoi_exit_bitmap0;
272 u64 eoi_exit_bitmap1;
273 u64 eoi_exit_bitmap2;
274 u64 eoi_exit_bitmap3;
Bandan Das41ab9372017-08-03 15:54:43 -0400275 u64 eptp_list_address;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800276 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300277 u64 guest_physical_address;
278 u64 vmcs_link_pointer;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400279 u64 pml_address;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300280 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;
292 u64 padding64[8]; /* room for future expansion */
293 /*
294 * To allow migration of L1 (complete with its L2 guests) between
295 * machines of different natural widths (32 or 64 bit), we cannot have
296 * unsigned long fields with no explict size. We use u64 (aliased
297 * natural_width) instead. Luckily, x86 is little-endian.
298 */
299 natural_width cr0_guest_host_mask;
300 natural_width cr4_guest_host_mask;
301 natural_width cr0_read_shadow;
302 natural_width cr4_read_shadow;
303 natural_width cr3_target_value0;
304 natural_width cr3_target_value1;
305 natural_width cr3_target_value2;
306 natural_width cr3_target_value3;
307 natural_width exit_qualification;
308 natural_width guest_linear_address;
309 natural_width guest_cr0;
310 natural_width guest_cr3;
311 natural_width guest_cr4;
312 natural_width guest_es_base;
313 natural_width guest_cs_base;
314 natural_width guest_ss_base;
315 natural_width guest_ds_base;
316 natural_width guest_fs_base;
317 natural_width guest_gs_base;
318 natural_width guest_ldtr_base;
319 natural_width guest_tr_base;
320 natural_width guest_gdtr_base;
321 natural_width guest_idtr_base;
322 natural_width guest_dr7;
323 natural_width guest_rsp;
324 natural_width guest_rip;
325 natural_width guest_rflags;
326 natural_width guest_pending_dbg_exceptions;
327 natural_width guest_sysenter_esp;
328 natural_width guest_sysenter_eip;
329 natural_width host_cr0;
330 natural_width host_cr3;
331 natural_width host_cr4;
332 natural_width host_fs_base;
333 natural_width host_gs_base;
334 natural_width host_tr_base;
335 natural_width host_gdtr_base;
336 natural_width host_idtr_base;
337 natural_width host_ia32_sysenter_esp;
338 natural_width host_ia32_sysenter_eip;
339 natural_width host_rsp;
340 natural_width host_rip;
341 natural_width paddingl[8]; /* room for future expansion */
342 u32 pin_based_vm_exec_control;
343 u32 cpu_based_vm_exec_control;
344 u32 exception_bitmap;
345 u32 page_fault_error_code_mask;
346 u32 page_fault_error_code_match;
347 u32 cr3_target_count;
348 u32 vm_exit_controls;
349 u32 vm_exit_msr_store_count;
350 u32 vm_exit_msr_load_count;
351 u32 vm_entry_controls;
352 u32 vm_entry_msr_load_count;
353 u32 vm_entry_intr_info_field;
354 u32 vm_entry_exception_error_code;
355 u32 vm_entry_instruction_len;
356 u32 tpr_threshold;
357 u32 secondary_vm_exec_control;
358 u32 vm_instruction_error;
359 u32 vm_exit_reason;
360 u32 vm_exit_intr_info;
361 u32 vm_exit_intr_error_code;
362 u32 idt_vectoring_info_field;
363 u32 idt_vectoring_error_code;
364 u32 vm_exit_instruction_len;
365 u32 vmx_instruction_info;
366 u32 guest_es_limit;
367 u32 guest_cs_limit;
368 u32 guest_ss_limit;
369 u32 guest_ds_limit;
370 u32 guest_fs_limit;
371 u32 guest_gs_limit;
372 u32 guest_ldtr_limit;
373 u32 guest_tr_limit;
374 u32 guest_gdtr_limit;
375 u32 guest_idtr_limit;
376 u32 guest_es_ar_bytes;
377 u32 guest_cs_ar_bytes;
378 u32 guest_ss_ar_bytes;
379 u32 guest_ds_ar_bytes;
380 u32 guest_fs_ar_bytes;
381 u32 guest_gs_ar_bytes;
382 u32 guest_ldtr_ar_bytes;
383 u32 guest_tr_ar_bytes;
384 u32 guest_interruptibility_info;
385 u32 guest_activity_state;
386 u32 guest_sysenter_cs;
387 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100388 u32 vmx_preemption_timer_value;
389 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300390 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800391 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300392 u16 guest_es_selector;
393 u16 guest_cs_selector;
394 u16 guest_ss_selector;
395 u16 guest_ds_selector;
396 u16 guest_fs_selector;
397 u16 guest_gs_selector;
398 u16 guest_ldtr_selector;
399 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800400 u16 guest_intr_status;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400401 u16 guest_pml_index;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300402 u16 host_es_selector;
403 u16 host_cs_selector;
404 u16 host_ss_selector;
405 u16 host_ds_selector;
406 u16 host_fs_selector;
407 u16 host_gs_selector;
408 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300409};
410
411/*
412 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
413 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
414 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
415 */
416#define VMCS12_REVISION 0x11e57ed0
417
418/*
419 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
420 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
421 * current implementation, 4K are reserved to avoid future complications.
422 */
423#define VMCS12_SIZE 0x1000
424
425/*
Jim Mattson5b157062017-12-22 12:11:12 -0800426 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
427 * supported VMCS12 field encoding.
428 */
429#define VMCS12_MAX_FIELD_INDEX 0x17
430
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100431struct nested_vmx_msrs {
432 /*
433 * We only store the "true" versions of the VMX capability MSRs. We
434 * generate the "non-true" versions by setting the must-be-1 bits
435 * according to the SDM.
436 */
437 u32 procbased_ctls_low;
438 u32 procbased_ctls_high;
439 u32 secondary_ctls_low;
440 u32 secondary_ctls_high;
441 u32 pinbased_ctls_low;
442 u32 pinbased_ctls_high;
443 u32 exit_ctls_low;
444 u32 exit_ctls_high;
445 u32 entry_ctls_low;
446 u32 entry_ctls_high;
447 u32 misc_low;
448 u32 misc_high;
449 u32 ept_caps;
450 u32 vpid_caps;
451 u64 basic;
452 u64 cr0_fixed0;
453 u64 cr0_fixed1;
454 u64 cr4_fixed0;
455 u64 cr4_fixed1;
456 u64 vmcs_enum;
457 u64 vmfunc_controls;
458};
459
Jim Mattson5b157062017-12-22 12:11:12 -0800460/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300461 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
462 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
463 */
464struct nested_vmx {
465 /* Has the level1 guest done vmxon? */
466 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400467 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400468 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300469
470 /* The guest-physical address of the current VMCS L1 keeps for L2 */
471 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700472 /*
473 * Cache of the guest's VMCS, existing outside of guest memory.
474 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700475 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700476 */
477 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300478 /*
479 * Indicates if the shadow vmcs must be updated with the
480 * data hold by vmcs12
481 */
482 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100483 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300484
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200485 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300486 /* L2 must run next, and mustn't decide to exit to L1. */
487 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600488
489 struct loaded_vmcs vmcs02;
490
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300491 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600492 * Guest pages referred to in the vmcs02 with host-physical
493 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300494 */
495 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800496 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800497 struct page *pi_desc_page;
498 struct pi_desc *pi_desc;
499 bool pi_pending;
500 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100501
502 struct hrtimer preemption_timer;
503 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200504
505 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
506 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800507
Wanpeng Li5c614b32015-10-13 09:18:36 -0700508 u16 vpid02;
509 u16 last_vpid;
510
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100511 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200512
513 /* SMM related state */
514 struct {
515 /* in VMX operation on SMM entry? */
516 bool vmxon;
517 /* in guest mode on SMM entry? */
518 bool guest_mode;
519 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300520};
521
Yang Zhang01e439b2013-04-11 19:25:12 +0800522#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800523#define POSTED_INTR_SN 1
524
Yang Zhang01e439b2013-04-11 19:25:12 +0800525/* Posted-Interrupt Descriptor */
526struct pi_desc {
527 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800528 union {
529 struct {
530 /* bit 256 - Outstanding Notification */
531 u16 on : 1,
532 /* bit 257 - Suppress Notification */
533 sn : 1,
534 /* bit 271:258 - Reserved */
535 rsvd_1 : 14;
536 /* bit 279:272 - Notification Vector */
537 u8 nv;
538 /* bit 287:280 - Reserved */
539 u8 rsvd_2;
540 /* bit 319:288 - Notification Destination */
541 u32 ndst;
542 };
543 u64 control;
544 };
545 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800546} __aligned(64);
547
Yang Zhanga20ed542013-04-11 19:25:15 +0800548static bool pi_test_and_set_on(struct pi_desc *pi_desc)
549{
550 return test_and_set_bit(POSTED_INTR_ON,
551 (unsigned long *)&pi_desc->control);
552}
553
554static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
555{
556 return test_and_clear_bit(POSTED_INTR_ON,
557 (unsigned long *)&pi_desc->control);
558}
559
560static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
561{
562 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
563}
564
Feng Wuebbfc762015-09-18 22:29:46 +0800565static inline void pi_clear_sn(struct pi_desc *pi_desc)
566{
567 return clear_bit(POSTED_INTR_SN,
568 (unsigned long *)&pi_desc->control);
569}
570
571static inline void pi_set_sn(struct pi_desc *pi_desc)
572{
573 return set_bit(POSTED_INTR_SN,
574 (unsigned long *)&pi_desc->control);
575}
576
Paolo Bonziniad361092016-09-20 16:15:05 +0200577static inline void pi_clear_on(struct pi_desc *pi_desc)
578{
579 clear_bit(POSTED_INTR_ON,
580 (unsigned long *)&pi_desc->control);
581}
582
Feng Wuebbfc762015-09-18 22:29:46 +0800583static inline int pi_test_on(struct pi_desc *pi_desc)
584{
585 return test_bit(POSTED_INTR_ON,
586 (unsigned long *)&pi_desc->control);
587}
588
589static inline int pi_test_sn(struct pi_desc *pi_desc)
590{
591 return test_bit(POSTED_INTR_SN,
592 (unsigned long *)&pi_desc->control);
593}
594
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400595struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000596 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300597 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300598 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100599 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300600 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200601 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200602 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300603 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400604 int nmsrs;
605 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800606 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400607#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300608 u64 msr_host_kernel_gs_base;
609 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400610#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100611
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100612 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100613 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100614
Gleb Natapov2961e8762013-11-25 15:37:13 +0200615 u32 vm_entry_controls_shadow;
616 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200617 u32 secondary_exec_control;
618
Nadav Har'Eld462b812011-05-24 15:26:10 +0300619 /*
620 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
621 * non-nested (L1) guest, it always points to vmcs01. For a nested
622 * guest (L2), it points to a different VMCS.
623 */
624 struct loaded_vmcs vmcs01;
625 struct loaded_vmcs *loaded_vmcs;
626 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300627 struct msr_autoload {
628 unsigned nr;
629 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
630 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
631 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400632 struct {
633 int loaded;
634 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300635#ifdef CONFIG_X86_64
636 u16 ds_sel, es_sel;
637#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200638 int gs_ldt_reload_needed;
639 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000640 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400641 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200642 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300643 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300644 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300645 struct kvm_segment segs[8];
646 } rmode;
647 struct {
648 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300649 struct kvm_save_segment {
650 u16 selector;
651 unsigned long base;
652 u32 limit;
653 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300654 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300655 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800656 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300657 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200658
Andi Kleena0861c02009-06-08 17:37:09 +0800659 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800660
Yang Zhang01e439b2013-04-11 19:25:12 +0800661 /* Posted interrupt descriptor */
662 struct pi_desc pi_desc;
663
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300664 /* Support for a guest hypervisor (nested VMX) */
665 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200666
667 /* Dynamic PLE window. */
668 int ple_window;
669 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800670
671 /* Support for PML */
672#define PML_ENTITY_NUM 512
673 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800674
Yunhong Jiang64672c92016-06-13 14:19:59 -0700675 /* apic deadline value in host tsc */
676 u64 hv_deadline_tsc;
677
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800678 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800679
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800680 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800681
Wanpeng Li74c55932017-11-29 01:31:20 -0800682 unsigned long host_debugctlmsr;
683
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800684 /*
685 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
686 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
687 * in msr_ia32_feature_control_valid_bits.
688 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800689 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800690 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400691};
692
Avi Kivity2fb92db2011-04-27 19:42:18 +0300693enum segment_cache_field {
694 SEG_FIELD_SEL = 0,
695 SEG_FIELD_BASE = 1,
696 SEG_FIELD_LIMIT = 2,
697 SEG_FIELD_AR = 3,
698
699 SEG_FIELD_NR = 4
700};
701
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700702static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
703{
704 return container_of(kvm, struct kvm_vmx, kvm);
705}
706
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400707static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
708{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000709 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400710}
711
Feng Wuefc64402015-09-18 22:29:51 +0800712static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
713{
714 return &(to_vmx(vcpu)->pi_desc);
715}
716
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800717#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300718#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800719#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
720#define FIELD64(number, name) \
721 FIELD(number, name), \
722 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300723
Abel Gordon4607c2d2013-04-18 14:35:55 +0300724
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100725static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100726#define SHADOW_FIELD_RO(x) x,
727#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300728};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400729static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300730 ARRAY_SIZE(shadow_read_only_fields);
731
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100732static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100733#define SHADOW_FIELD_RW(x) x,
734#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300735};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400736static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300737 ARRAY_SIZE(shadow_read_write_fields);
738
Mathias Krause772e0312012-08-30 01:30:19 +0200739static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300740 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800741 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300742 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
743 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
744 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
745 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
746 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
747 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
748 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
749 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800750 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400751 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300752 FIELD(HOST_ES_SELECTOR, host_es_selector),
753 FIELD(HOST_CS_SELECTOR, host_cs_selector),
754 FIELD(HOST_SS_SELECTOR, host_ss_selector),
755 FIELD(HOST_DS_SELECTOR, host_ds_selector),
756 FIELD(HOST_FS_SELECTOR, host_fs_selector),
757 FIELD(HOST_GS_SELECTOR, host_gs_selector),
758 FIELD(HOST_TR_SELECTOR, host_tr_selector),
759 FIELD64(IO_BITMAP_A, io_bitmap_a),
760 FIELD64(IO_BITMAP_B, io_bitmap_b),
761 FIELD64(MSR_BITMAP, msr_bitmap),
762 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
763 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
764 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
765 FIELD64(TSC_OFFSET, tsc_offset),
766 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
767 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800768 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400769 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300770 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800771 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
772 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
773 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
774 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400775 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800776 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300777 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
778 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400779 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300780 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
781 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
782 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
783 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
784 FIELD64(GUEST_PDPTR0, guest_pdptr0),
785 FIELD64(GUEST_PDPTR1, guest_pdptr1),
786 FIELD64(GUEST_PDPTR2, guest_pdptr2),
787 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100788 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300789 FIELD64(HOST_IA32_PAT, host_ia32_pat),
790 FIELD64(HOST_IA32_EFER, host_ia32_efer),
791 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
792 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
793 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
794 FIELD(EXCEPTION_BITMAP, exception_bitmap),
795 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
796 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
797 FIELD(CR3_TARGET_COUNT, cr3_target_count),
798 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
799 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
800 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
801 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
802 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
803 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
804 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
805 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
806 FIELD(TPR_THRESHOLD, tpr_threshold),
807 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
808 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
809 FIELD(VM_EXIT_REASON, vm_exit_reason),
810 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
811 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
812 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
813 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
814 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
815 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
816 FIELD(GUEST_ES_LIMIT, guest_es_limit),
817 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
818 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
819 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
820 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
821 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
822 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
823 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
824 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
825 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
826 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
827 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
828 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
829 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
830 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
831 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
832 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
833 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
834 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
835 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
836 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
837 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100838 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300839 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
840 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
841 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
842 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
843 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
844 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
845 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
846 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
847 FIELD(EXIT_QUALIFICATION, exit_qualification),
848 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
849 FIELD(GUEST_CR0, guest_cr0),
850 FIELD(GUEST_CR3, guest_cr3),
851 FIELD(GUEST_CR4, guest_cr4),
852 FIELD(GUEST_ES_BASE, guest_es_base),
853 FIELD(GUEST_CS_BASE, guest_cs_base),
854 FIELD(GUEST_SS_BASE, guest_ss_base),
855 FIELD(GUEST_DS_BASE, guest_ds_base),
856 FIELD(GUEST_FS_BASE, guest_fs_base),
857 FIELD(GUEST_GS_BASE, guest_gs_base),
858 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
859 FIELD(GUEST_TR_BASE, guest_tr_base),
860 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
861 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
862 FIELD(GUEST_DR7, guest_dr7),
863 FIELD(GUEST_RSP, guest_rsp),
864 FIELD(GUEST_RIP, guest_rip),
865 FIELD(GUEST_RFLAGS, guest_rflags),
866 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
867 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
868 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
869 FIELD(HOST_CR0, host_cr0),
870 FIELD(HOST_CR3, host_cr3),
871 FIELD(HOST_CR4, host_cr4),
872 FIELD(HOST_FS_BASE, host_fs_base),
873 FIELD(HOST_GS_BASE, host_gs_base),
874 FIELD(HOST_TR_BASE, host_tr_base),
875 FIELD(HOST_GDTR_BASE, host_gdtr_base),
876 FIELD(HOST_IDTR_BASE, host_idtr_base),
877 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
878 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
879 FIELD(HOST_RSP, host_rsp),
880 FIELD(HOST_RIP, host_rip),
881};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300882
883static inline short vmcs_field_to_offset(unsigned long field)
884{
Dan Williams085331d2018-01-31 17:47:03 -0800885 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
886 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800887 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100888
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800889 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -0800890 return -ENOENT;
891
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800892 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -0800893 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -0800894 return -ENOENT;
895
Linus Torvalds15303ba2018-02-10 13:16:35 -0800896 index = array_index_nospec(index, size);
897 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -0800898 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100899 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -0800900 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300901}
902
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300903static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
904{
David Matlack4f2777b2016-07-13 17:16:37 -0700905 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300906}
907
Peter Feiner995f00a2017-06-30 17:26:32 -0700908static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300909static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700910static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800911static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300912static void vmx_set_segment(struct kvm_vcpu *vcpu,
913 struct kvm_segment *var, int seg);
914static void vmx_get_segment(struct kvm_vcpu *vcpu,
915 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200916static bool guest_state_valid(struct kvm_vcpu *vcpu);
917static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300918static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200919static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
920static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
921static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
922 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100923static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +0100924static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
925 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300926
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927static DEFINE_PER_CPU(struct vmcs *, vmxarea);
928static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300929/*
930 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
931 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
932 */
933static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Feng Wubf9f6ac2015-09-18 22:29:55 +0800935/*
936 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
937 * can find which vCPU should be waken up.
938 */
939static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
940static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
941
Radim Krčmář23611332016-09-29 22:41:33 +0200942enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200943 VMX_VMREAD_BITMAP,
944 VMX_VMWRITE_BITMAP,
945 VMX_BITMAP_NR
946};
947
948static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
949
Radim Krčmář23611332016-09-29 22:41:33 +0200950#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
951#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300952
Avi Kivity110312c2010-12-21 12:54:20 +0200953static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200954static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200955
Sheng Yang2384d2b2008-01-17 15:14:33 +0800956static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
957static DEFINE_SPINLOCK(vmx_vpid_lock);
958
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300959static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 int size;
961 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300962 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300964 u32 pin_based_exec_ctrl;
965 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800966 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300967 u32 vmexit_ctrl;
968 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +0100969 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300970} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
Hannes Ederefff9e52008-11-28 17:02:06 +0100972static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800973 u32 ept;
974 u32 vpid;
975} vmx_capability;
976
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977#define VMX_SEGMENT_FIELD(seg) \
978 [VCPU_SREG_##seg] = { \
979 .selector = GUEST_##seg##_SELECTOR, \
980 .base = GUEST_##seg##_BASE, \
981 .limit = GUEST_##seg##_LIMIT, \
982 .ar_bytes = GUEST_##seg##_AR_BYTES, \
983 }
984
Mathias Krause772e0312012-08-30 01:30:19 +0200985static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 unsigned selector;
987 unsigned base;
988 unsigned limit;
989 unsigned ar_bytes;
990} kvm_vmx_segment_fields[] = {
991 VMX_SEGMENT_FIELD(CS),
992 VMX_SEGMENT_FIELD(DS),
993 VMX_SEGMENT_FIELD(ES),
994 VMX_SEGMENT_FIELD(FS),
995 VMX_SEGMENT_FIELD(GS),
996 VMX_SEGMENT_FIELD(SS),
997 VMX_SEGMENT_FIELD(TR),
998 VMX_SEGMENT_FIELD(LDTR),
999};
1000
Avi Kivity26bb0982009-09-07 11:14:12 +03001001static u64 host_efer;
1002
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001003static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1004
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001005/*
Brian Gerst8c065852010-07-17 09:03:26 -04001006 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001007 * away by decrementing the array size.
1008 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001010#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001011 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001013 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001016DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1017
1018#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1019
1020#define KVM_EVMCS_VERSION 1
1021
1022#if IS_ENABLED(CONFIG_HYPERV)
1023static bool __read_mostly enlightened_vmcs = true;
1024module_param(enlightened_vmcs, bool, 0444);
1025
1026static inline void evmcs_write64(unsigned long field, u64 value)
1027{
1028 u16 clean_field;
1029 int offset = get_evmcs_offset(field, &clean_field);
1030
1031 if (offset < 0)
1032 return;
1033
1034 *(u64 *)((char *)current_evmcs + offset) = value;
1035
1036 current_evmcs->hv_clean_fields &= ~clean_field;
1037}
1038
1039static inline void evmcs_write32(unsigned long field, u32 value)
1040{
1041 u16 clean_field;
1042 int offset = get_evmcs_offset(field, &clean_field);
1043
1044 if (offset < 0)
1045 return;
1046
1047 *(u32 *)((char *)current_evmcs + offset) = value;
1048 current_evmcs->hv_clean_fields &= ~clean_field;
1049}
1050
1051static inline void evmcs_write16(unsigned long field, u16 value)
1052{
1053 u16 clean_field;
1054 int offset = get_evmcs_offset(field, &clean_field);
1055
1056 if (offset < 0)
1057 return;
1058
1059 *(u16 *)((char *)current_evmcs + offset) = value;
1060 current_evmcs->hv_clean_fields &= ~clean_field;
1061}
1062
1063static inline u64 evmcs_read64(unsigned long field)
1064{
1065 int offset = get_evmcs_offset(field, NULL);
1066
1067 if (offset < 0)
1068 return 0;
1069
1070 return *(u64 *)((char *)current_evmcs + offset);
1071}
1072
1073static inline u32 evmcs_read32(unsigned long field)
1074{
1075 int offset = get_evmcs_offset(field, NULL);
1076
1077 if (offset < 0)
1078 return 0;
1079
1080 return *(u32 *)((char *)current_evmcs + offset);
1081}
1082
1083static inline u16 evmcs_read16(unsigned long field)
1084{
1085 int offset = get_evmcs_offset(field, NULL);
1086
1087 if (offset < 0)
1088 return 0;
1089
1090 return *(u16 *)((char *)current_evmcs + offset);
1091}
1092
1093static void evmcs_load(u64 phys_addr)
1094{
1095 struct hv_vp_assist_page *vp_ap =
1096 hv_get_vp_assist_page(smp_processor_id());
1097
1098 vp_ap->current_nested_vmcs = phys_addr;
1099 vp_ap->enlighten_vmentry = 1;
1100}
1101
1102static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1103{
1104 /*
1105 * Enlightened VMCSv1 doesn't support these:
1106 *
1107 * POSTED_INTR_NV = 0x00000002,
1108 * GUEST_INTR_STATUS = 0x00000810,
1109 * APIC_ACCESS_ADDR = 0x00002014,
1110 * POSTED_INTR_DESC_ADDR = 0x00002016,
1111 * EOI_EXIT_BITMAP0 = 0x0000201c,
1112 * EOI_EXIT_BITMAP1 = 0x0000201e,
1113 * EOI_EXIT_BITMAP2 = 0x00002020,
1114 * EOI_EXIT_BITMAP3 = 0x00002022,
1115 */
1116 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1117 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1118 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1119 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1120 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1121 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1122 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1123
1124 /*
1125 * GUEST_PML_INDEX = 0x00000812,
1126 * PML_ADDRESS = 0x0000200e,
1127 */
1128 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1129
1130 /* VM_FUNCTION_CONTROL = 0x00002018, */
1131 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1132
1133 /*
1134 * EPTP_LIST_ADDRESS = 0x00002024,
1135 * VMREAD_BITMAP = 0x00002026,
1136 * VMWRITE_BITMAP = 0x00002028,
1137 */
1138 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1139
1140 /*
1141 * TSC_MULTIPLIER = 0x00002032,
1142 */
1143 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1144
1145 /*
1146 * PLE_GAP = 0x00004020,
1147 * PLE_WINDOW = 0x00004022,
1148 */
1149 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1150
1151 /*
1152 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1153 */
1154 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1155
1156 /*
1157 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1158 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1159 */
1160 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1161 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1162
1163 /*
1164 * Currently unsupported in KVM:
1165 * GUEST_IA32_RTIT_CTL = 0x00002814,
1166 */
1167}
1168#else /* !IS_ENABLED(CONFIG_HYPERV) */
1169static inline void evmcs_write64(unsigned long field, u64 value) {}
1170static inline void evmcs_write32(unsigned long field, u32 value) {}
1171static inline void evmcs_write16(unsigned long field, u16 value) {}
1172static inline u64 evmcs_read64(unsigned long field) { return 0; }
1173static inline u32 evmcs_read32(unsigned long field) { return 0; }
1174static inline u16 evmcs_read16(unsigned long field) { return 0; }
1175static inline void evmcs_load(u64 phys_addr) {}
1176static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
1177#endif /* IS_ENABLED(CONFIG_HYPERV) */
1178
Jan Kiszka5bb16012016-02-09 20:14:21 +01001179static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180{
1181 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1182 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001183 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1184}
1185
Jan Kiszka6f054852016-02-09 20:15:18 +01001186static inline bool is_debug(u32 intr_info)
1187{
1188 return is_exception_n(intr_info, DB_VECTOR);
1189}
1190
1191static inline bool is_breakpoint(u32 intr_info)
1192{
1193 return is_exception_n(intr_info, BP_VECTOR);
1194}
1195
Jan Kiszka5bb16012016-02-09 20:14:21 +01001196static inline bool is_page_fault(u32 intr_info)
1197{
1198 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199}
1200
Gui Jianfeng31299942010-03-15 17:29:09 +08001201static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001202{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001203 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001204}
1205
Gui Jianfeng31299942010-03-15 17:29:09 +08001206static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001207{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001208 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001209}
1210
Liran Alon9e869482018-03-12 13:12:51 +02001211static inline bool is_gp_fault(u32 intr_info)
1212{
1213 return is_exception_n(intr_info, GP_VECTOR);
1214}
1215
Gui Jianfeng31299942010-03-15 17:29:09 +08001216static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217{
1218 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1219 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1220}
1221
Gui Jianfeng31299942010-03-15 17:29:09 +08001222static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001223{
1224 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1225 INTR_INFO_VALID_MASK)) ==
1226 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1227}
1228
Gui Jianfeng31299942010-03-15 17:29:09 +08001229static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001230{
Sheng Yang04547152009-04-01 15:52:31 +08001231 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001232}
1233
Gui Jianfeng31299942010-03-15 17:29:09 +08001234static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001235{
Sheng Yang04547152009-04-01 15:52:31 +08001236 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001237}
1238
Paolo Bonzini35754c92015-07-29 12:05:37 +02001239static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001240{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001241 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001242}
1243
Gui Jianfeng31299942010-03-15 17:29:09 +08001244static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001245{
Sheng Yang04547152009-04-01 15:52:31 +08001246 return vmcs_config.cpu_based_exec_ctrl &
1247 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001248}
1249
Avi Kivity774ead32007-12-26 13:57:04 +02001250static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001251{
Sheng Yang04547152009-04-01 15:52:31 +08001252 return vmcs_config.cpu_based_2nd_exec_ctrl &
1253 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1254}
1255
Yang Zhang8d146952013-01-25 10:18:50 +08001256static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1257{
1258 return vmcs_config.cpu_based_2nd_exec_ctrl &
1259 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1260}
1261
Yang Zhang83d4c282013-01-25 10:18:49 +08001262static inline bool cpu_has_vmx_apic_register_virt(void)
1263{
1264 return vmcs_config.cpu_based_2nd_exec_ctrl &
1265 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1266}
1267
Yang Zhangc7c9c562013-01-25 10:18:51 +08001268static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1269{
1270 return vmcs_config.cpu_based_2nd_exec_ctrl &
1271 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1272}
1273
Yunhong Jiang64672c92016-06-13 14:19:59 -07001274/*
1275 * Comment's format: document - errata name - stepping - processor name.
1276 * Refer from
1277 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1278 */
1279static u32 vmx_preemption_cpu_tfms[] = {
1280/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12810x000206E6,
1282/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1283/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1284/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
12850x00020652,
1286/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
12870x00020655,
1288/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1289/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1290/*
1291 * 320767.pdf - AAP86 - B1 -
1292 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1293 */
12940x000106E5,
1295/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
12960x000106A0,
1297/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
12980x000106A1,
1299/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
13000x000106A4,
1301 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1302 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1303 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
13040x000106A5,
1305};
1306
1307static inline bool cpu_has_broken_vmx_preemption_timer(void)
1308{
1309 u32 eax = cpuid_eax(0x00000001), i;
1310
1311 /* Clear the reserved bits */
1312 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001313 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001314 if (eax == vmx_preemption_cpu_tfms[i])
1315 return true;
1316
1317 return false;
1318}
1319
1320static inline bool cpu_has_vmx_preemption_timer(void)
1321{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001322 return vmcs_config.pin_based_exec_ctrl &
1323 PIN_BASED_VMX_PREEMPTION_TIMER;
1324}
1325
Yang Zhang01e439b2013-04-11 19:25:12 +08001326static inline bool cpu_has_vmx_posted_intr(void)
1327{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001328 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1329 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001330}
1331
1332static inline bool cpu_has_vmx_apicv(void)
1333{
1334 return cpu_has_vmx_apic_register_virt() &&
1335 cpu_has_vmx_virtual_intr_delivery() &&
1336 cpu_has_vmx_posted_intr();
1337}
1338
Sheng Yang04547152009-04-01 15:52:31 +08001339static inline bool cpu_has_vmx_flexpriority(void)
1340{
1341 return cpu_has_vmx_tpr_shadow() &&
1342 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001343}
1344
Marcelo Tosattie7997942009-06-11 12:07:40 -03001345static inline bool cpu_has_vmx_ept_execute_only(void)
1346{
Gui Jianfeng31299942010-03-15 17:29:09 +08001347 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001348}
1349
Marcelo Tosattie7997942009-06-11 12:07:40 -03001350static inline bool cpu_has_vmx_ept_2m_page(void)
1351{
Gui Jianfeng31299942010-03-15 17:29:09 +08001352 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001353}
1354
Sheng Yang878403b2010-01-05 19:02:29 +08001355static inline bool cpu_has_vmx_ept_1g_page(void)
1356{
Gui Jianfeng31299942010-03-15 17:29:09 +08001357 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001358}
1359
Sheng Yang4bc9b982010-06-02 14:05:24 +08001360static inline bool cpu_has_vmx_ept_4levels(void)
1361{
1362 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1363}
1364
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001365static inline bool cpu_has_vmx_ept_mt_wb(void)
1366{
1367 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1368}
1369
Yu Zhang855feb62017-08-24 20:27:55 +08001370static inline bool cpu_has_vmx_ept_5levels(void)
1371{
1372 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1373}
1374
Xudong Hao83c3a332012-05-28 19:33:35 +08001375static inline bool cpu_has_vmx_ept_ad_bits(void)
1376{
1377 return vmx_capability.ept & VMX_EPT_AD_BIT;
1378}
1379
Gui Jianfeng31299942010-03-15 17:29:09 +08001380static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001381{
Gui Jianfeng31299942010-03-15 17:29:09 +08001382 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001383}
1384
Gui Jianfeng31299942010-03-15 17:29:09 +08001385static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001386{
Gui Jianfeng31299942010-03-15 17:29:09 +08001387 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001388}
1389
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001390static inline bool cpu_has_vmx_invvpid_single(void)
1391{
1392 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1393}
1394
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001395static inline bool cpu_has_vmx_invvpid_global(void)
1396{
1397 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1398}
1399
Wanpeng Li08d839c2017-03-23 05:30:08 -07001400static inline bool cpu_has_vmx_invvpid(void)
1401{
1402 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1403}
1404
Gui Jianfeng31299942010-03-15 17:29:09 +08001405static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001406{
Sheng Yang04547152009-04-01 15:52:31 +08001407 return vmcs_config.cpu_based_2nd_exec_ctrl &
1408 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001409}
1410
Gui Jianfeng31299942010-03-15 17:29:09 +08001411static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001412{
1413 return vmcs_config.cpu_based_2nd_exec_ctrl &
1414 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1415}
1416
Gui Jianfeng31299942010-03-15 17:29:09 +08001417static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001418{
1419 return vmcs_config.cpu_based_2nd_exec_ctrl &
1420 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1421}
1422
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001423static inline bool cpu_has_vmx_basic_inout(void)
1424{
1425 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1426}
1427
Paolo Bonzini35754c92015-07-29 12:05:37 +02001428static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001429{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001430 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001431}
1432
Gui Jianfeng31299942010-03-15 17:29:09 +08001433static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001434{
Sheng Yang04547152009-04-01 15:52:31 +08001435 return vmcs_config.cpu_based_2nd_exec_ctrl &
1436 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001437}
1438
Gui Jianfeng31299942010-03-15 17:29:09 +08001439static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001440{
1441 return vmcs_config.cpu_based_2nd_exec_ctrl &
1442 SECONDARY_EXEC_RDTSCP;
1443}
1444
Mao, Junjiead756a12012-07-02 01:18:48 +00001445static inline bool cpu_has_vmx_invpcid(void)
1446{
1447 return vmcs_config.cpu_based_2nd_exec_ctrl &
1448 SECONDARY_EXEC_ENABLE_INVPCID;
1449}
1450
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001451static inline bool cpu_has_virtual_nmis(void)
1452{
1453 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1454}
1455
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001456static inline bool cpu_has_vmx_wbinvd_exit(void)
1457{
1458 return vmcs_config.cpu_based_2nd_exec_ctrl &
1459 SECONDARY_EXEC_WBINVD_EXITING;
1460}
1461
Abel Gordonabc4fc52013-04-18 14:35:25 +03001462static inline bool cpu_has_vmx_shadow_vmcs(void)
1463{
1464 u64 vmx_msr;
1465 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1466 /* check if the cpu supports writing r/o exit information fields */
1467 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1468 return false;
1469
1470 return vmcs_config.cpu_based_2nd_exec_ctrl &
1471 SECONDARY_EXEC_SHADOW_VMCS;
1472}
1473
Kai Huang843e4332015-01-28 10:54:28 +08001474static inline bool cpu_has_vmx_pml(void)
1475{
1476 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1477}
1478
Haozhong Zhang64903d62015-10-20 15:39:09 +08001479static inline bool cpu_has_vmx_tsc_scaling(void)
1480{
1481 return vmcs_config.cpu_based_2nd_exec_ctrl &
1482 SECONDARY_EXEC_TSC_SCALING;
1483}
1484
Bandan Das2a499e42017-08-03 15:54:41 -04001485static inline bool cpu_has_vmx_vmfunc(void)
1486{
1487 return vmcs_config.cpu_based_2nd_exec_ctrl &
1488 SECONDARY_EXEC_ENABLE_VMFUNC;
1489}
1490
Sheng Yang04547152009-04-01 15:52:31 +08001491static inline bool report_flexpriority(void)
1492{
1493 return flexpriority_enabled;
1494}
1495
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001496static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1497{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001498 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001499}
1500
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001501static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1502{
1503 return vmcs12->cpu_based_vm_exec_control & bit;
1504}
1505
1506static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1507{
1508 return (vmcs12->cpu_based_vm_exec_control &
1509 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1510 (vmcs12->secondary_vm_exec_control & bit);
1511}
1512
Jan Kiszkaf41245002014-03-07 20:03:13 +01001513static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1514{
1515 return vmcs12->pin_based_vm_exec_control &
1516 PIN_BASED_VMX_PREEMPTION_TIMER;
1517}
1518
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001519static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1520{
1521 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1522}
1523
1524static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1525{
1526 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1527}
1528
Nadav Har'El155a97a2013-08-05 11:07:16 +03001529static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1530{
1531 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1532}
1533
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001534static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1535{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001536 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001537}
1538
Bandan Dasc5f983f2017-05-05 15:25:14 -04001539static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1540{
1541 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1542}
1543
Wincy Vanf2b93282015-02-03 23:56:03 +08001544static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1545{
1546 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1547}
1548
Wanpeng Li5c614b32015-10-13 09:18:36 -07001549static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1550{
1551 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1552}
1553
Wincy Van82f0dd42015-02-03 23:57:18 +08001554static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1555{
1556 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1557}
1558
Wincy Van608406e2015-02-03 23:57:51 +08001559static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1560{
1561 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1562}
1563
Wincy Van705699a2015-02-03 23:58:17 +08001564static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1565{
1566 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1567}
1568
Bandan Das27c42a12017-08-03 15:54:42 -04001569static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1570{
1571 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1572}
1573
Bandan Das41ab9372017-08-03 15:54:43 -04001574static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1575{
1576 return nested_cpu_has_vmfunc(vmcs12) &&
1577 (vmcs12->vm_function_control &
1578 VMX_VMFUNC_EPTP_SWITCHING);
1579}
1580
Jim Mattsonef85b672016-12-12 11:01:37 -08001581static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001582{
1583 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001584 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001585}
1586
Jan Kiszka533558b2014-01-04 18:47:20 +01001587static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1588 u32 exit_intr_info,
1589 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001590static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1591 struct vmcs12 *vmcs12,
1592 u32 reason, unsigned long qualification);
1593
Rusty Russell8b9cf982007-07-30 16:31:43 +10001594static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001595{
1596 int i;
1597
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001598 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001599 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001600 return i;
1601 return -1;
1602}
1603
Sheng Yang2384d2b2008-01-17 15:14:33 +08001604static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1605{
1606 struct {
1607 u64 vpid : 16;
1608 u64 rsvd : 48;
1609 u64 gva;
1610 } operand = { vpid, 0, gva };
1611
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001612 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001613 /* CF==1 or ZF==1 --> rc = -1 */
1614 "; ja 1f ; ud2 ; 1:"
1615 : : "a"(&operand), "c"(ext) : "cc", "memory");
1616}
1617
Sheng Yang14394422008-04-28 12:24:45 +08001618static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1619{
1620 struct {
1621 u64 eptp, gpa;
1622 } operand = {eptp, gpa};
1623
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001624 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001625 /* CF==1 or ZF==1 --> rc = -1 */
1626 "; ja 1f ; ud2 ; 1:\n"
1627 : : "a" (&operand), "c" (ext) : "cc", "memory");
1628}
1629
Avi Kivity26bb0982009-09-07 11:14:12 +03001630static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001631{
1632 int i;
1633
Rusty Russell8b9cf982007-07-30 16:31:43 +10001634 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001635 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001636 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001637 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001638}
1639
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640static void vmcs_clear(struct vmcs *vmcs)
1641{
1642 u64 phys_addr = __pa(vmcs);
1643 u8 error;
1644
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001645 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001646 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647 : "cc", "memory");
1648 if (error)
1649 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1650 vmcs, phys_addr);
1651}
1652
Nadav Har'Eld462b812011-05-24 15:26:10 +03001653static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1654{
1655 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001656 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1657 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001658 loaded_vmcs->cpu = -1;
1659 loaded_vmcs->launched = 0;
1660}
1661
Dongxiao Xu7725b892010-05-11 18:29:38 +08001662static void vmcs_load(struct vmcs *vmcs)
1663{
1664 u64 phys_addr = __pa(vmcs);
1665 u8 error;
1666
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001667 if (static_branch_unlikely(&enable_evmcs))
1668 return evmcs_load(phys_addr);
1669
Dongxiao Xu7725b892010-05-11 18:29:38 +08001670 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001671 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001672 : "cc", "memory");
1673 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001674 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001675 vmcs, phys_addr);
1676}
1677
Dave Young2965faa2015-09-09 15:38:55 -07001678#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001679/*
1680 * This bitmap is used to indicate whether the vmclear
1681 * operation is enabled on all cpus. All disabled by
1682 * default.
1683 */
1684static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1685
1686static inline void crash_enable_local_vmclear(int cpu)
1687{
1688 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1689}
1690
1691static inline void crash_disable_local_vmclear(int cpu)
1692{
1693 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1694}
1695
1696static inline int crash_local_vmclear_enabled(int cpu)
1697{
1698 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1699}
1700
1701static void crash_vmclear_local_loaded_vmcss(void)
1702{
1703 int cpu = raw_smp_processor_id();
1704 struct loaded_vmcs *v;
1705
1706 if (!crash_local_vmclear_enabled(cpu))
1707 return;
1708
1709 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1710 loaded_vmcss_on_cpu_link)
1711 vmcs_clear(v->vmcs);
1712}
1713#else
1714static inline void crash_enable_local_vmclear(int cpu) { }
1715static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001716#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001717
Nadav Har'Eld462b812011-05-24 15:26:10 +03001718static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001720 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001721 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722
Nadav Har'Eld462b812011-05-24 15:26:10 +03001723 if (loaded_vmcs->cpu != cpu)
1724 return; /* vcpu migration can race with cpu offline */
1725 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001727 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001728 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001729
1730 /*
1731 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1732 * is before setting loaded_vmcs->vcpu to -1 which is done in
1733 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1734 * then adds the vmcs into percpu list before it is deleted.
1735 */
1736 smp_wmb();
1737
Nadav Har'Eld462b812011-05-24 15:26:10 +03001738 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001739 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740}
1741
Nadav Har'Eld462b812011-05-24 15:26:10 +03001742static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001743{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001744 int cpu = loaded_vmcs->cpu;
1745
1746 if (cpu != -1)
1747 smp_call_function_single(cpu,
1748 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001749}
1750
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001751static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001752{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001753 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001754 return;
1755
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001756 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001757 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001758}
1759
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001760static inline void vpid_sync_vcpu_global(void)
1761{
1762 if (cpu_has_vmx_invvpid_global())
1763 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1764}
1765
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001766static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001767{
1768 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001769 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001770 else
1771 vpid_sync_vcpu_global();
1772}
1773
Sheng Yang14394422008-04-28 12:24:45 +08001774static inline void ept_sync_global(void)
1775{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001776 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001777}
1778
1779static inline void ept_sync_context(u64 eptp)
1780{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001781 if (cpu_has_vmx_invept_context())
1782 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1783 else
1784 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001785}
1786
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001787static __always_inline void vmcs_check16(unsigned long field)
1788{
1789 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1790 "16-bit accessor invalid for 64-bit field");
1791 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1792 "16-bit accessor invalid for 64-bit high field");
1793 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1794 "16-bit accessor invalid for 32-bit high field");
1795 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1796 "16-bit accessor invalid for natural width field");
1797}
1798
1799static __always_inline void vmcs_check32(unsigned long field)
1800{
1801 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1802 "32-bit accessor invalid for 16-bit field");
1803 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1804 "32-bit accessor invalid for natural width field");
1805}
1806
1807static __always_inline void vmcs_check64(unsigned long field)
1808{
1809 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1810 "64-bit accessor invalid for 16-bit field");
1811 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1812 "64-bit accessor invalid for 64-bit high field");
1813 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1814 "64-bit accessor invalid for 32-bit field");
1815 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1816 "64-bit accessor invalid for natural width field");
1817}
1818
1819static __always_inline void vmcs_checkl(unsigned long field)
1820{
1821 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1822 "Natural width accessor invalid for 16-bit field");
1823 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1824 "Natural width accessor invalid for 64-bit field");
1825 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1826 "Natural width accessor invalid for 64-bit high field");
1827 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1828 "Natural width accessor invalid for 32-bit field");
1829}
1830
1831static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832{
Avi Kivity5e520e62011-05-15 10:13:12 -04001833 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834
Avi Kivity5e520e62011-05-15 10:13:12 -04001835 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1836 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837 return value;
1838}
1839
Avi Kivity96304212011-05-15 10:13:13 -04001840static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001842 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001843 if (static_branch_unlikely(&enable_evmcs))
1844 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001845 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846}
1847
Avi Kivity96304212011-05-15 10:13:13 -04001848static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001850 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001851 if (static_branch_unlikely(&enable_evmcs))
1852 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001853 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854}
1855
Avi Kivity96304212011-05-15 10:13:13 -04001856static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001858 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001859 if (static_branch_unlikely(&enable_evmcs))
1860 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001861#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001862 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001864 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865#endif
1866}
1867
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001868static __always_inline unsigned long vmcs_readl(unsigned long field)
1869{
1870 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001871 if (static_branch_unlikely(&enable_evmcs))
1872 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001873 return __vmcs_readl(field);
1874}
1875
Avi Kivitye52de1b2007-01-05 16:36:56 -08001876static noinline void vmwrite_error(unsigned long field, unsigned long value)
1877{
1878 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1879 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1880 dump_stack();
1881}
1882
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001883static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884{
1885 u8 error;
1886
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001887 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001888 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001889 if (unlikely(error))
1890 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891}
1892
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001893static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001894{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001895 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001896 if (static_branch_unlikely(&enable_evmcs))
1897 return evmcs_write16(field, value);
1898
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001899 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900}
1901
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001902static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001904 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001905 if (static_branch_unlikely(&enable_evmcs))
1906 return evmcs_write32(field, value);
1907
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001908 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909}
1910
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001911static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001913 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001914 if (static_branch_unlikely(&enable_evmcs))
1915 return evmcs_write64(field, value);
1916
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001917 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001918#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001920 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921#endif
1922}
1923
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001924static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001925{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001926 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001927 if (static_branch_unlikely(&enable_evmcs))
1928 return evmcs_write64(field, value);
1929
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001930 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001931}
1932
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001933static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001934{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001935 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1936 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001937 if (static_branch_unlikely(&enable_evmcs))
1938 return evmcs_write32(field, evmcs_read32(field) & ~mask);
1939
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001940 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1941}
1942
1943static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1944{
1945 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1946 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001947 if (static_branch_unlikely(&enable_evmcs))
1948 return evmcs_write32(field, evmcs_read32(field) | mask);
1949
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001950 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001951}
1952
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001953static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1954{
1955 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1956}
1957
Gleb Natapov2961e8762013-11-25 15:37:13 +02001958static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1959{
1960 vmcs_write32(VM_ENTRY_CONTROLS, val);
1961 vmx->vm_entry_controls_shadow = val;
1962}
1963
1964static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1965{
1966 if (vmx->vm_entry_controls_shadow != val)
1967 vm_entry_controls_init(vmx, val);
1968}
1969
1970static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1971{
1972 return vmx->vm_entry_controls_shadow;
1973}
1974
1975
1976static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1977{
1978 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1979}
1980
1981static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1982{
1983 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1984}
1985
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001986static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1987{
1988 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1989}
1990
Gleb Natapov2961e8762013-11-25 15:37:13 +02001991static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1992{
1993 vmcs_write32(VM_EXIT_CONTROLS, val);
1994 vmx->vm_exit_controls_shadow = val;
1995}
1996
1997static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1998{
1999 if (vmx->vm_exit_controls_shadow != val)
2000 vm_exit_controls_init(vmx, val);
2001}
2002
2003static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2004{
2005 return vmx->vm_exit_controls_shadow;
2006}
2007
2008
2009static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2010{
2011 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2012}
2013
2014static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2015{
2016 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2017}
2018
Avi Kivity2fb92db2011-04-27 19:42:18 +03002019static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2020{
2021 vmx->segment_cache.bitmask = 0;
2022}
2023
2024static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2025 unsigned field)
2026{
2027 bool ret;
2028 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2029
2030 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2031 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2032 vmx->segment_cache.bitmask = 0;
2033 }
2034 ret = vmx->segment_cache.bitmask & mask;
2035 vmx->segment_cache.bitmask |= mask;
2036 return ret;
2037}
2038
2039static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2040{
2041 u16 *p = &vmx->segment_cache.seg[seg].selector;
2042
2043 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2044 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2045 return *p;
2046}
2047
2048static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2049{
2050 ulong *p = &vmx->segment_cache.seg[seg].base;
2051
2052 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2053 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2054 return *p;
2055}
2056
2057static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2058{
2059 u32 *p = &vmx->segment_cache.seg[seg].limit;
2060
2061 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2062 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2063 return *p;
2064}
2065
2066static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2067{
2068 u32 *p = &vmx->segment_cache.seg[seg].ar;
2069
2070 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2071 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2072 return *p;
2073}
2074
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002075static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2076{
2077 u32 eb;
2078
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002079 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002080 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002081 /*
2082 * Guest access to VMware backdoor ports could legitimately
2083 * trigger #GP because of TSS I/O permission bitmap.
2084 * We intercept those #GP and allow access to them anyway
2085 * as VMware does.
2086 */
2087 if (enable_vmware_backdoor)
2088 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002089 if ((vcpu->guest_debug &
2090 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2091 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2092 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002093 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002094 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002095 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002096 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002097
2098 /* When we are running a nested L2 guest and L1 specified for it a
2099 * certain exception bitmap, we must trap the same exceptions and pass
2100 * them to L1. When running L2, we will only handle the exceptions
2101 * specified above if L1 did not want them.
2102 */
2103 if (is_guest_mode(vcpu))
2104 eb |= get_vmcs12(vcpu)->exception_bitmap;
2105
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002106 vmcs_write32(EXCEPTION_BITMAP, eb);
2107}
2108
Ashok Raj15d45072018-02-01 22:59:43 +01002109/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002110 * Check if MSR is intercepted for currently loaded MSR bitmap.
2111 */
2112static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2113{
2114 unsigned long *msr_bitmap;
2115 int f = sizeof(unsigned long);
2116
2117 if (!cpu_has_vmx_msr_bitmap())
2118 return true;
2119
2120 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2121
2122 if (msr <= 0x1fff) {
2123 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2124 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2125 msr &= 0x1fff;
2126 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2127 }
2128
2129 return true;
2130}
2131
2132/*
Ashok Raj15d45072018-02-01 22:59:43 +01002133 * Check if MSR is intercepted for L01 MSR bitmap.
2134 */
2135static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2136{
2137 unsigned long *msr_bitmap;
2138 int f = sizeof(unsigned long);
2139
2140 if (!cpu_has_vmx_msr_bitmap())
2141 return true;
2142
2143 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2144
2145 if (msr <= 0x1fff) {
2146 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2147 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2148 msr &= 0x1fff;
2149 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2150 }
2151
2152 return true;
2153}
2154
Gleb Natapov2961e8762013-11-25 15:37:13 +02002155static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2156 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002157{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002158 vm_entry_controls_clearbit(vmx, entry);
2159 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002160}
2161
Avi Kivity61d2ef22010-04-28 16:40:38 +03002162static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2163{
2164 unsigned i;
2165 struct msr_autoload *m = &vmx->msr_autoload;
2166
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002167 switch (msr) {
2168 case MSR_EFER:
2169 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002170 clear_atomic_switch_msr_special(vmx,
2171 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002172 VM_EXIT_LOAD_IA32_EFER);
2173 return;
2174 }
2175 break;
2176 case MSR_CORE_PERF_GLOBAL_CTRL:
2177 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002178 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002179 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2180 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2181 return;
2182 }
2183 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002184 }
2185
Avi Kivity61d2ef22010-04-28 16:40:38 +03002186 for (i = 0; i < m->nr; ++i)
2187 if (m->guest[i].index == msr)
2188 break;
2189
2190 if (i == m->nr)
2191 return;
2192 --m->nr;
2193 m->guest[i] = m->guest[m->nr];
2194 m->host[i] = m->host[m->nr];
2195 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2196 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2197}
2198
Gleb Natapov2961e8762013-11-25 15:37:13 +02002199static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2200 unsigned long entry, unsigned long exit,
2201 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2202 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002203{
2204 vmcs_write64(guest_val_vmcs, guest_val);
2205 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002206 vm_entry_controls_setbit(vmx, entry);
2207 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002208}
2209
Avi Kivity61d2ef22010-04-28 16:40:38 +03002210static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2211 u64 guest_val, u64 host_val)
2212{
2213 unsigned i;
2214 struct msr_autoload *m = &vmx->msr_autoload;
2215
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002216 switch (msr) {
2217 case MSR_EFER:
2218 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002219 add_atomic_switch_msr_special(vmx,
2220 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002221 VM_EXIT_LOAD_IA32_EFER,
2222 GUEST_IA32_EFER,
2223 HOST_IA32_EFER,
2224 guest_val, host_val);
2225 return;
2226 }
2227 break;
2228 case MSR_CORE_PERF_GLOBAL_CTRL:
2229 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002230 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002231 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2232 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2233 GUEST_IA32_PERF_GLOBAL_CTRL,
2234 HOST_IA32_PERF_GLOBAL_CTRL,
2235 guest_val, host_val);
2236 return;
2237 }
2238 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002239 case MSR_IA32_PEBS_ENABLE:
2240 /* PEBS needs a quiescent period after being disabled (to write
2241 * a record). Disabling PEBS through VMX MSR swapping doesn't
2242 * provide that period, so a CPU could write host's record into
2243 * guest's memory.
2244 */
2245 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002246 }
2247
Avi Kivity61d2ef22010-04-28 16:40:38 +03002248 for (i = 0; i < m->nr; ++i)
2249 if (m->guest[i].index == msr)
2250 break;
2251
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002252 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002253 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002254 "Can't add msr %x\n", msr);
2255 return;
2256 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002257 ++m->nr;
2258 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2259 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2260 }
2261
2262 m->guest[i].index = msr;
2263 m->guest[i].value = guest_val;
2264 m->host[i].index = msr;
2265 m->host[i].value = host_val;
2266}
2267
Avi Kivity92c0d902009-10-29 11:00:16 +02002268static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002269{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002270 u64 guest_efer = vmx->vcpu.arch.efer;
2271 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002272
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002273 if (!enable_ept) {
2274 /*
2275 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2276 * host CPUID is more efficient than testing guest CPUID
2277 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2278 */
2279 if (boot_cpu_has(X86_FEATURE_SMEP))
2280 guest_efer |= EFER_NX;
2281 else if (!(guest_efer & EFER_NX))
2282 ignore_bits |= EFER_NX;
2283 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002284
Avi Kivity51c6cf62007-08-29 03:48:05 +03002285 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002286 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002287 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002288 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002289#ifdef CONFIG_X86_64
2290 ignore_bits |= EFER_LMA | EFER_LME;
2291 /* SCE is meaningful only in long mode on Intel */
2292 if (guest_efer & EFER_LMA)
2293 ignore_bits &= ~(u64)EFER_SCE;
2294#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002295
2296 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002297
2298 /*
2299 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2300 * On CPUs that support "load IA32_EFER", always switch EFER
2301 * atomically, since it's faster than switching it manually.
2302 */
2303 if (cpu_has_load_ia32_efer ||
2304 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002305 if (!(guest_efer & EFER_LMA))
2306 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002307 if (guest_efer != host_efer)
2308 add_atomic_switch_msr(vmx, MSR_EFER,
2309 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002310 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002311 } else {
2312 guest_efer &= ~ignore_bits;
2313 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002314
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002315 vmx->guest_msrs[efer_offset].data = guest_efer;
2316 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2317
2318 return true;
2319 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002320}
2321
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002322#ifdef CONFIG_X86_32
2323/*
2324 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2325 * VMCS rather than the segment table. KVM uses this helper to figure
2326 * out the current bases to poke them into the VMCS before entry.
2327 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002328static unsigned long segment_base(u16 selector)
2329{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002330 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002331 unsigned long v;
2332
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002333 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002334 return 0;
2335
Thomas Garnier45fc8752017-03-14 10:05:08 -07002336 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002337
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002338 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002339 u16 ldt_selector = kvm_read_ldt();
2340
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002341 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002342 return 0;
2343
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002344 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002345 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002346 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002347 return v;
2348}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002349#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002350
Avi Kivity04d2cc72007-09-10 18:10:54 +03002351static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002352{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002353 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002354 int cpu = raw_smp_processor_id();
Avi Kivity26bb0982009-09-07 11:14:12 +03002355 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002356
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002357 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002358 return;
2359
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002360 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002361 /*
2362 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2363 * allow segment selectors with cpl > 0 or ti == 1.
2364 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002365 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002366 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002367
2368#ifdef CONFIG_X86_64
2369 save_fsgs_for_kvm();
2370 vmx->host_state.fs_sel = current->thread.fsindex;
2371 vmx->host_state.gs_sel = current->thread.gsindex;
2372#else
Avi Kivity9581d442010-10-19 16:46:55 +02002373 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002374 savesegment(gs, vmx->host_state.gs_sel);
2375#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002376 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002377 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002378 vmx->host_state.fs_reload_needed = 0;
2379 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002380 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002381 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002382 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002383 if (!(vmx->host_state.gs_sel & 7))
2384 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002385 else {
2386 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002387 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002388 }
2389
2390#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002391 savesegment(ds, vmx->host_state.ds_sel);
2392 savesegment(es, vmx->host_state.es_sel);
2393#endif
2394
2395#ifdef CONFIG_X86_64
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002396 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002397 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity33ed6322007-05-02 16:54:03 +03002398#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002399 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2400 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002401#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002402
2403#ifdef CONFIG_X86_64
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002404 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002405 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002406 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002407#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002408 if (boot_cpu_has(X86_FEATURE_MPX))
2409 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002410 for (i = 0; i < vmx->save_nmsrs; ++i)
2411 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002412 vmx->guest_msrs[i].data,
2413 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002414}
2415
Avi Kivitya9b21b62008-06-24 11:48:49 +03002416static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002417{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002418 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002419 return;
2420
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002421 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002422 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002423#ifdef CONFIG_X86_64
2424 if (is_long_mode(&vmx->vcpu))
2425 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2426#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002427 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002428 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002429#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002430 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002431#else
2432 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002433#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002434 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002435 if (vmx->host_state.fs_reload_needed)
2436 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002437#ifdef CONFIG_X86_64
2438 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2439 loadsegment(ds, vmx->host_state.ds_sel);
2440 loadsegment(es, vmx->host_state.es_sel);
2441 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002442#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002443 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002444#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002445 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002446#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002447 if (vmx->host_state.msr_host_bndcfgs)
2448 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002449 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002450}
2451
Avi Kivitya9b21b62008-06-24 11:48:49 +03002452static void vmx_load_host_state(struct vcpu_vmx *vmx)
2453{
2454 preempt_disable();
2455 __vmx_load_host_state(vmx);
2456 preempt_enable();
2457}
2458
Feng Wu28b835d2015-09-18 22:29:54 +08002459static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2460{
2461 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2462 struct pi_desc old, new;
2463 unsigned int dest;
2464
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002465 /*
2466 * In case of hot-plug or hot-unplug, we may have to undo
2467 * vmx_vcpu_pi_put even if there is no assigned device. And we
2468 * always keep PI.NDST up to date for simplicity: it makes the
2469 * code easier, and CPU migration is not a fast path.
2470 */
2471 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002472 return;
2473
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002474 /*
2475 * First handle the simple case where no cmpxchg is necessary; just
2476 * allow posting non-urgent interrupts.
2477 *
2478 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2479 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2480 * expects the VCPU to be on the blocked_vcpu_list that matches
2481 * PI.NDST.
2482 */
2483 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2484 vcpu->cpu == cpu) {
2485 pi_clear_sn(pi_desc);
2486 return;
2487 }
2488
2489 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002490 do {
2491 old.control = new.control = pi_desc->control;
2492
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002493 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002494
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002495 if (x2apic_enabled())
2496 new.ndst = dest;
2497 else
2498 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002499
Feng Wu28b835d2015-09-18 22:29:54 +08002500 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002501 } while (cmpxchg64(&pi_desc->control, old.control,
2502 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002503}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002504
Peter Feinerc95ba922016-08-17 09:36:47 -07002505static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2506{
2507 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2508 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2509}
2510
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511/*
2512 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2513 * vcpu mutex is already taken.
2514 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002515static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002517 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002518 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002520 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002521 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002522 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002523 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002524
2525 /*
2526 * Read loaded_vmcs->cpu should be before fetching
2527 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2528 * See the comments in __loaded_vmcs_clear().
2529 */
2530 smp_rmb();
2531
Nadav Har'Eld462b812011-05-24 15:26:10 +03002532 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2533 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002534 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002535 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002536 }
2537
2538 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2539 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2540 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002541 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002542 }
2543
2544 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002545 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002546 unsigned long sysenter_esp;
2547
2548 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002549
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 /*
2551 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002552 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002554 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002555 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002556 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002558 /*
2559 * VM exits change the host TR limit to 0x67 after a VM
2560 * exit. This is okay, since 0x67 covers everything except
2561 * the IO bitmap and have have code to handle the IO bitmap
2562 * being lost after a VM exit.
2563 */
2564 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2565
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2567 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002568
Nadav Har'Eld462b812011-05-24 15:26:10 +03002569 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 }
Feng Wu28b835d2015-09-18 22:29:54 +08002571
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002572 /* Setup TSC multiplier */
2573 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002574 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2575 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002576
Feng Wu28b835d2015-09-18 22:29:54 +08002577 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002578 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002579 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002580}
2581
2582static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2583{
2584 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2585
2586 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002587 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2588 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002589 return;
2590
2591 /* Set SN when the vCPU is preempted */
2592 if (vcpu->preempted)
2593 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594}
2595
2596static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2597{
Feng Wu28b835d2015-09-18 22:29:54 +08002598 vmx_vcpu_pi_put(vcpu);
2599
Avi Kivitya9b21b62008-06-24 11:48:49 +03002600 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601}
2602
Wanpeng Lif244dee2017-07-20 01:11:54 -07002603static bool emulation_required(struct kvm_vcpu *vcpu)
2604{
2605 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2606}
2607
Avi Kivityedcafe32009-12-30 18:07:40 +02002608static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2609
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002610/*
2611 * Return the cr0 value that a nested guest would read. This is a combination
2612 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2613 * its hypervisor (cr0_read_shadow).
2614 */
2615static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2616{
2617 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2618 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2619}
2620static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2621{
2622 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2623 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2624}
2625
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2627{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002628 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002629
Avi Kivity6de12732011-03-07 12:51:22 +02002630 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2631 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2632 rflags = vmcs_readl(GUEST_RFLAGS);
2633 if (to_vmx(vcpu)->rmode.vm86_active) {
2634 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2635 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2636 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2637 }
2638 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002639 }
Avi Kivity6de12732011-03-07 12:51:22 +02002640 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641}
2642
2643static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2644{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002645 unsigned long old_rflags = vmx_get_rflags(vcpu);
2646
Avi Kivity6de12732011-03-07 12:51:22 +02002647 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2648 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002649 if (to_vmx(vcpu)->rmode.vm86_active) {
2650 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002651 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002652 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002654
2655 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2656 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657}
2658
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002659static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002660{
2661 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2662 int ret = 0;
2663
2664 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002665 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002666 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002667 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002668
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002669 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002670}
2671
2672static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2673{
2674 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2675 u32 interruptibility = interruptibility_old;
2676
2677 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2678
Jan Kiszka48005f62010-02-19 19:38:07 +01002679 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002680 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002681 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002682 interruptibility |= GUEST_INTR_STATE_STI;
2683
2684 if ((interruptibility != interruptibility_old))
2685 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2686}
2687
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2689{
2690 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002692 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002694 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695
Glauber Costa2809f5d2009-05-12 16:21:05 -04002696 /* skipping an emulated instruction also counts */
2697 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698}
2699
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002700static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2701 unsigned long exit_qual)
2702{
2703 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2704 unsigned int nr = vcpu->arch.exception.nr;
2705 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2706
2707 if (vcpu->arch.exception.has_error_code) {
2708 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2709 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2710 }
2711
2712 if (kvm_exception_is_soft(nr))
2713 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2714 else
2715 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2716
2717 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2718 vmx_get_nmi_mask(vcpu))
2719 intr_info |= INTR_INFO_UNBLOCK_NMI;
2720
2721 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2722}
2723
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002724/*
2725 * KVM wants to inject page-faults which it got to the guest. This function
2726 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002727 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002728static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002729{
2730 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002731 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002732
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002733 if (nr == PF_VECTOR) {
2734 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002735 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002736 return 1;
2737 }
2738 /*
2739 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2740 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2741 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2742 * can be written only when inject_pending_event runs. This should be
2743 * conditional on a new capability---if the capability is disabled,
2744 * kvm_multiple_exception would write the ancillary information to
2745 * CR2 or DR6, for backwards ABI-compatibility.
2746 */
2747 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2748 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002749 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002750 return 1;
2751 }
2752 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002753 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002754 if (nr == DB_VECTOR)
2755 *exit_qual = vcpu->arch.dr6;
2756 else
2757 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002758 return 1;
2759 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002760 }
2761
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002762 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002763}
2764
Wanpeng Licaa057a2018-03-12 04:53:03 -07002765static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2766{
2767 /*
2768 * Ensure that we clear the HLT state in the VMCS. We don't need to
2769 * explicitly skip the instruction because if the HLT state is set,
2770 * then the instruction is already executing and RIP has already been
2771 * advanced.
2772 */
2773 if (kvm_hlt_in_guest(vcpu->kvm) &&
2774 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2775 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2776}
2777
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002778static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002779{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002780 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002781 unsigned nr = vcpu->arch.exception.nr;
2782 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002783 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002784 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002785
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002786 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002787 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002788 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2789 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002790
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002791 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002792 int inc_eip = 0;
2793 if (kvm_exception_is_soft(nr))
2794 inc_eip = vcpu->arch.event_exit_inst_len;
2795 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002796 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002797 return;
2798 }
2799
Sean Christophersonadd5ff72018-03-23 09:34:00 -07002800 WARN_ON_ONCE(vmx->emulation_required);
2801
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002802 if (kvm_exception_is_soft(nr)) {
2803 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2804 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002805 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2806 } else
2807 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2808
2809 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002810
2811 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02002812}
2813
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002814static bool vmx_rdtscp_supported(void)
2815{
2816 return cpu_has_vmx_rdtscp();
2817}
2818
Mao, Junjiead756a12012-07-02 01:18:48 +00002819static bool vmx_invpcid_supported(void)
2820{
2821 return cpu_has_vmx_invpcid() && enable_ept;
2822}
2823
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824/*
Eddie Donga75beee2007-05-17 18:55:15 +03002825 * Swap MSR entry in host/guest MSR entry array.
2826 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002827static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002828{
Avi Kivity26bb0982009-09-07 11:14:12 +03002829 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002830
2831 tmp = vmx->guest_msrs[to];
2832 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2833 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002834}
2835
2836/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002837 * Set up the vmcs to automatically save and restore system
2838 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2839 * mode, as fiddling with msrs is very expensive.
2840 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002841static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002842{
Avi Kivity26bb0982009-09-07 11:14:12 +03002843 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002844
Eddie Donga75beee2007-05-17 18:55:15 +03002845 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002846#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002847 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002848 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002849 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002850 move_msr_up(vmx, index, save_nmsrs++);
2851 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002852 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002853 move_msr_up(vmx, index, save_nmsrs++);
2854 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002855 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002856 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002857 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002858 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002859 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002860 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002861 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002862 * if efer.sce is enabled.
2863 */
Brian Gerst8c065852010-07-17 09:03:26 -04002864 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002865 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002866 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002867 }
Eddie Donga75beee2007-05-17 18:55:15 +03002868#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002869 index = __find_msr_index(vmx, MSR_EFER);
2870 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002871 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002872
Avi Kivity26bb0982009-09-07 11:14:12 +03002873 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002874
Yang Zhang8d146952013-01-25 10:18:50 +08002875 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002876 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002877}
2878
2879/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002881 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2882 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002884static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885{
2886 u64 host_tsc, tsc_offset;
2887
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002888 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002890 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891}
2892
2893/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002894 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002896static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002898 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002899 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002900 * We're here if L1 chose not to trap WRMSR to TSC. According
2901 * to the spec, this should set L1's TSC; The offset that L1
2902 * set for L2 remains unchanged, and still needs to be added
2903 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002904 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002905 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002906 /* recalculate vmcs02.TSC_OFFSET: */
2907 vmcs12 = get_vmcs12(vcpu);
2908 vmcs_write64(TSC_OFFSET, offset +
2909 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2910 vmcs12->tsc_offset : 0));
2911 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002912 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2913 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002914 vmcs_write64(TSC_OFFSET, offset);
2915 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916}
2917
Nadav Har'El801d3422011-05-25 23:02:23 +03002918/*
2919 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2920 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2921 * all guests if the "nested" module option is off, and can also be disabled
2922 * for a single guest by disabling its VMX cpuid bit.
2923 */
2924static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2925{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002926 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002927}
2928
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002930 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2931 * returned for the various VMX controls MSRs when nested VMX is enabled.
2932 * The same values should also be used to verify that vmcs12 control fields are
2933 * valid during nested entry from L1 to L2.
2934 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2935 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2936 * bit in the high half is on if the corresponding bit in the control field
2937 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002938 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002939static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002940{
Paolo Bonzini13893092018-02-26 13:40:09 +01002941 if (!nested) {
2942 memset(msrs, 0, sizeof(*msrs));
2943 return;
2944 }
2945
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002946 /*
2947 * Note that as a general rule, the high half of the MSRs (bits in
2948 * the control fields which may be 1) should be initialized by the
2949 * intersection of the underlying hardware's MSR (i.e., features which
2950 * can be supported) and the list of features we want to expose -
2951 * because they are known to be properly supported in our code.
2952 * Also, usually, the low half of the MSRs (bits which must be 1) can
2953 * be set to 0, meaning that L1 may turn off any of these bits. The
2954 * reason is that if one of these bits is necessary, it will appear
2955 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2956 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002957 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002958 * These rules have exceptions below.
2959 */
2960
2961 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002962 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002963 msrs->pinbased_ctls_low,
2964 msrs->pinbased_ctls_high);
2965 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002966 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002967 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002968 PIN_BASED_EXT_INTR_MASK |
2969 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01002970 PIN_BASED_VIRTUAL_NMIS |
2971 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002972 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002973 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002974 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002975
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002976 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002977 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002978 msrs->exit_ctls_low,
2979 msrs->exit_ctls_high);
2980 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002981 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002982
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002983 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002984#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002985 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002986#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002987 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002988 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002989 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002990 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002991 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2992
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002993 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002994 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002995
Jan Kiszka2996fca2014-06-16 13:59:43 +02002996 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002997 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002998
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002999 /* entry controls */
3000 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003001 msrs->entry_ctls_low,
3002 msrs->entry_ctls_high);
3003 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003004 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003005 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003006#ifdef CONFIG_X86_64
3007 VM_ENTRY_IA32E_MODE |
3008#endif
3009 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003010 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003011 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003012 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003013 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003014
Jan Kiszka2996fca2014-06-16 13:59:43 +02003015 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003016 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003017
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003018 /* cpu-based controls */
3019 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003020 msrs->procbased_ctls_low,
3021 msrs->procbased_ctls_high);
3022 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003023 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003024 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003025 CPU_BASED_VIRTUAL_INTR_PENDING |
3026 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003027 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3028 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3029 CPU_BASED_CR3_STORE_EXITING |
3030#ifdef CONFIG_X86_64
3031 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3032#endif
3033 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003034 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3035 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3036 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3037 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003038 /*
3039 * We can allow some features even when not supported by the
3040 * hardware. For example, L1 can specify an MSR bitmap - and we
3041 * can use it to avoid exits to L1 - even when L0 runs L2
3042 * without MSR bitmaps.
3043 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003044 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003045 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003046 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003047
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003048 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003049 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003050 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3051
Paolo Bonzini80154d72017-08-24 13:55:35 +02003052 /*
3053 * secondary cpu-based controls. Do not include those that
3054 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3055 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003056 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003057 msrs->secondary_ctls_low,
3058 msrs->secondary_ctls_high);
3059 msrs->secondary_ctls_low = 0;
3060 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003061 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003062 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003063 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003064 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003065 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003066 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003067
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003068 if (enable_ept) {
3069 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003070 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003071 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003072 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003073 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003074 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003075 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003076 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003077 msrs->ept_caps &= vmx_capability.ept;
3078 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003079 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3080 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003081 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003082 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003083 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003084 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003085 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003086 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003087
Bandan Das27c42a12017-08-03 15:54:42 -04003088 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003089 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003090 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003091 /*
3092 * Advertise EPTP switching unconditionally
3093 * since we emulate it
3094 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003095 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003096 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003097 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003098 }
3099
Paolo Bonzinief697a72016-03-18 16:58:38 +01003100 /*
3101 * Old versions of KVM use the single-context version without
3102 * checking for support, so declare that it is supported even
3103 * though it is treated as global context. The alternative is
3104 * not failing the single-context invvpid, and it is worse.
3105 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003106 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003107 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003108 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003109 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003110 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003111 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003112
Radim Krčmář0790ec12015-03-17 14:02:32 +01003113 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003114 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003115 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3116
Jan Kiszkac18911a2013-03-13 16:06:41 +01003117 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003118 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003119 msrs->misc_low,
3120 msrs->misc_high);
3121 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3122 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003123 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003124 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003125 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003126
3127 /*
3128 * This MSR reports some information about VMX support. We
3129 * should return information about the VMX we emulate for the
3130 * guest, and the VMCS structure we give it - not about the
3131 * VMX support of the underlying hardware.
3132 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003133 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003134 VMCS12_REVISION |
3135 VMX_BASIC_TRUE_CTLS |
3136 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3137 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3138
3139 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003140 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003141
3142 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003143 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003144 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3145 * We picked the standard core2 setting.
3146 */
3147#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3148#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003149 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3150 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003151
3152 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003153 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3154 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003155
3156 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003157 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003158}
3159
David Matlack38991522016-11-29 18:14:08 -08003160/*
3161 * if fixed0[i] == 1: val[i] must be 1
3162 * if fixed1[i] == 0: val[i] must be 0
3163 */
3164static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3165{
3166 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003167}
3168
3169static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3170{
David Matlack38991522016-11-29 18:14:08 -08003171 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003172}
3173
3174static inline u64 vmx_control_msr(u32 low, u32 high)
3175{
3176 return low | ((u64)high << 32);
3177}
3178
David Matlack62cc6b9d2016-11-29 18:14:07 -08003179static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3180{
3181 superset &= mask;
3182 subset &= mask;
3183
3184 return (superset | subset) == superset;
3185}
3186
3187static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3188{
3189 const u64 feature_and_reserved =
3190 /* feature (except bit 48; see below) */
3191 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3192 /* reserved */
3193 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003194 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003195
3196 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3197 return -EINVAL;
3198
3199 /*
3200 * KVM does not emulate a version of VMX that constrains physical
3201 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3202 */
3203 if (data & BIT_ULL(48))
3204 return -EINVAL;
3205
3206 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3207 vmx_basic_vmcs_revision_id(data))
3208 return -EINVAL;
3209
3210 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3211 return -EINVAL;
3212
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003213 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003214 return 0;
3215}
3216
3217static int
3218vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3219{
3220 u64 supported;
3221 u32 *lowp, *highp;
3222
3223 switch (msr_index) {
3224 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003225 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3226 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003227 break;
3228 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003229 lowp = &vmx->nested.msrs.procbased_ctls_low;
3230 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003231 break;
3232 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003233 lowp = &vmx->nested.msrs.exit_ctls_low;
3234 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003235 break;
3236 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003237 lowp = &vmx->nested.msrs.entry_ctls_low;
3238 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003239 break;
3240 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003241 lowp = &vmx->nested.msrs.secondary_ctls_low;
3242 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003243 break;
3244 default:
3245 BUG();
3246 }
3247
3248 supported = vmx_control_msr(*lowp, *highp);
3249
3250 /* Check must-be-1 bits are still 1. */
3251 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3252 return -EINVAL;
3253
3254 /* Check must-be-0 bits are still 0. */
3255 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3256 return -EINVAL;
3257
3258 *lowp = data;
3259 *highp = data >> 32;
3260 return 0;
3261}
3262
3263static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3264{
3265 const u64 feature_and_reserved_bits =
3266 /* feature */
3267 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3268 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3269 /* reserved */
3270 GENMASK_ULL(13, 9) | BIT_ULL(31);
3271 u64 vmx_misc;
3272
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003273 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3274 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003275
3276 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3277 return -EINVAL;
3278
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003279 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003280 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3281 vmx_misc_preemption_timer_rate(data) !=
3282 vmx_misc_preemption_timer_rate(vmx_misc))
3283 return -EINVAL;
3284
3285 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3286 return -EINVAL;
3287
3288 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3289 return -EINVAL;
3290
3291 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3292 return -EINVAL;
3293
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003294 vmx->nested.msrs.misc_low = data;
3295 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003296 return 0;
3297}
3298
3299static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3300{
3301 u64 vmx_ept_vpid_cap;
3302
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003303 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3304 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003305
3306 /* Every bit is either reserved or a feature bit. */
3307 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3308 return -EINVAL;
3309
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003310 vmx->nested.msrs.ept_caps = data;
3311 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003312 return 0;
3313}
3314
3315static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3316{
3317 u64 *msr;
3318
3319 switch (msr_index) {
3320 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003321 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003322 break;
3323 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003324 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003325 break;
3326 default:
3327 BUG();
3328 }
3329
3330 /*
3331 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3332 * must be 1 in the restored value.
3333 */
3334 if (!is_bitwise_subset(data, *msr, -1ULL))
3335 return -EINVAL;
3336
3337 *msr = data;
3338 return 0;
3339}
3340
3341/*
3342 * Called when userspace is restoring VMX MSRs.
3343 *
3344 * Returns 0 on success, non-0 otherwise.
3345 */
3346static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3347{
3348 struct vcpu_vmx *vmx = to_vmx(vcpu);
3349
3350 switch (msr_index) {
3351 case MSR_IA32_VMX_BASIC:
3352 return vmx_restore_vmx_basic(vmx, data);
3353 case MSR_IA32_VMX_PINBASED_CTLS:
3354 case MSR_IA32_VMX_PROCBASED_CTLS:
3355 case MSR_IA32_VMX_EXIT_CTLS:
3356 case MSR_IA32_VMX_ENTRY_CTLS:
3357 /*
3358 * The "non-true" VMX capability MSRs are generated from the
3359 * "true" MSRs, so we do not support restoring them directly.
3360 *
3361 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3362 * should restore the "true" MSRs with the must-be-1 bits
3363 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3364 * DEFAULT SETTINGS".
3365 */
3366 return -EINVAL;
3367 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3368 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3369 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3370 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3371 case MSR_IA32_VMX_PROCBASED_CTLS2:
3372 return vmx_restore_control_msr(vmx, msr_index, data);
3373 case MSR_IA32_VMX_MISC:
3374 return vmx_restore_vmx_misc(vmx, data);
3375 case MSR_IA32_VMX_CR0_FIXED0:
3376 case MSR_IA32_VMX_CR4_FIXED0:
3377 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3378 case MSR_IA32_VMX_CR0_FIXED1:
3379 case MSR_IA32_VMX_CR4_FIXED1:
3380 /*
3381 * These MSRs are generated based on the vCPU's CPUID, so we
3382 * do not support restoring them directly.
3383 */
3384 return -EINVAL;
3385 case MSR_IA32_VMX_EPT_VPID_CAP:
3386 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3387 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003388 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003389 return 0;
3390 default:
3391 /*
3392 * The rest of the VMX capability MSRs do not support restore.
3393 */
3394 return -EINVAL;
3395 }
3396}
3397
Jan Kiszkacae50132014-01-04 18:47:22 +01003398/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003399static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003400{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003401 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003402 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003403 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003404 break;
3405 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3406 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003407 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003408 msrs->pinbased_ctls_low,
3409 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003410 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3411 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003412 break;
3413 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3414 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003415 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003416 msrs->procbased_ctls_low,
3417 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003418 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3419 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003420 break;
3421 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3422 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003423 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003424 msrs->exit_ctls_low,
3425 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003426 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3427 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003428 break;
3429 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3430 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003431 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003432 msrs->entry_ctls_low,
3433 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003434 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3435 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003436 break;
3437 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003438 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003439 msrs->misc_low,
3440 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003441 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003442 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003443 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003444 break;
3445 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003446 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003447 break;
3448 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003449 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003450 break;
3451 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003452 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003453 break;
3454 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003455 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003456 break;
3457 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003458 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003459 msrs->secondary_ctls_low,
3460 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003461 break;
3462 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003463 *pdata = msrs->ept_caps |
3464 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003465 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003466 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003467 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003468 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003469 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003470 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003471 }
3472
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003473 return 0;
3474}
3475
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003476static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3477 uint64_t val)
3478{
3479 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3480
3481 return !(val & ~valid_bits);
3482}
3483
Tom Lendacky801e4592018-02-21 13:39:51 -06003484static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3485{
Paolo Bonzini13893092018-02-26 13:40:09 +01003486 switch (msr->index) {
3487 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3488 if (!nested)
3489 return 1;
3490 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3491 default:
3492 return 1;
3493 }
3494
3495 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003496}
3497
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003498/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499 * Reads an msr value (of 'msr_index') into 'pdata'.
3500 * Returns 0 on success, non-0 otherwise.
3501 * Assumes vcpu_load() was already called.
3502 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003503static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003505 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003506 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003508 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003509#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003511 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512 break;
3513 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003514 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003516 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003517 vmx_load_host_state(vmx);
3518 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003519 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003520#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003522 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303523 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003524 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003526 case MSR_IA32_SPEC_CTRL:
3527 if (!msr_info->host_initiated &&
3528 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3529 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3530 return 1;
3531
3532 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3533 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003534 case MSR_IA32_ARCH_CAPABILITIES:
3535 if (!msr_info->host_initiated &&
3536 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3537 return 1;
3538 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3539 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003541 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542 break;
3543 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003544 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545 break;
3546 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003547 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003548 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003549 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003550 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003551 (!msr_info->host_initiated &&
3552 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003553 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003554 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003555 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003556 case MSR_IA32_MCG_EXT_CTL:
3557 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003558 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003559 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003560 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003561 msr_info->data = vcpu->arch.mcg_ext_ctl;
3562 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003563 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003564 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003565 break;
3566 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3567 if (!nested_vmx_allowed(vcpu))
3568 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003569 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3570 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003571 case MSR_IA32_XSS:
3572 if (!vmx_xsaves_supported())
3573 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003574 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003575 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003576 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003577 if (!msr_info->host_initiated &&
3578 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003579 return 1;
3580 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003581 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003582 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003583 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003584 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003585 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003586 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003587 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003588 }
3589
Avi Kivity6aa8b732006-12-10 02:21:36 -08003590 return 0;
3591}
3592
Jan Kiszkacae50132014-01-04 18:47:22 +01003593static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3594
Avi Kivity6aa8b732006-12-10 02:21:36 -08003595/*
3596 * Writes msr value into into the appropriate "register".
3597 * Returns 0 on success, non-0 otherwise.
3598 * Assumes vcpu_load() was already called.
3599 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003600static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003601{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003602 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003603 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003604 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003605 u32 msr_index = msr_info->index;
3606 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003607
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003609 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003610 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003611 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003612#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003613 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003614 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615 vmcs_writel(GUEST_FS_BASE, data);
3616 break;
3617 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003618 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003619 vmcs_writel(GUEST_GS_BASE, data);
3620 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003621 case MSR_KERNEL_GS_BASE:
3622 vmx_load_host_state(vmx);
3623 vmx->msr_guest_kernel_gs_base = data;
3624 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003625#endif
3626 case MSR_IA32_SYSENTER_CS:
3627 vmcs_write32(GUEST_SYSENTER_CS, data);
3628 break;
3629 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003630 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003631 break;
3632 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003633 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003634 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003635 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003636 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003637 (!msr_info->host_initiated &&
3638 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003639 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003640 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003641 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003643 vmcs_write64(GUEST_BNDCFGS, data);
3644 break;
3645 case MSR_IA32_TSC:
3646 kvm_write_tsc(vcpu, msr_info);
3647 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003648 case MSR_IA32_SPEC_CTRL:
3649 if (!msr_info->host_initiated &&
3650 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3651 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3652 return 1;
3653
3654 /* The STIBP bit doesn't fault even if it's not advertised */
3655 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3656 return 1;
3657
3658 vmx->spec_ctrl = data;
3659
3660 if (!data)
3661 break;
3662
3663 /*
3664 * For non-nested:
3665 * When it's written (to non-zero) for the first time, pass
3666 * it through.
3667 *
3668 * For nested:
3669 * The handling of the MSR bitmap for L2 guests is done in
3670 * nested_vmx_merge_msr_bitmap. We should not touch the
3671 * vmcs02.msr_bitmap here since it gets completely overwritten
3672 * in the merging. We update the vmcs01 here for L1 as well
3673 * since it will end up touching the MSR anyway now.
3674 */
3675 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3676 MSR_IA32_SPEC_CTRL,
3677 MSR_TYPE_RW);
3678 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003679 case MSR_IA32_PRED_CMD:
3680 if (!msr_info->host_initiated &&
3681 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3682 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3683 return 1;
3684
3685 if (data & ~PRED_CMD_IBPB)
3686 return 1;
3687
3688 if (!data)
3689 break;
3690
3691 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3692
3693 /*
3694 * For non-nested:
3695 * When it's written (to non-zero) for the first time, pass
3696 * it through.
3697 *
3698 * For nested:
3699 * The handling of the MSR bitmap for L2 guests is done in
3700 * nested_vmx_merge_msr_bitmap. We should not touch the
3701 * vmcs02.msr_bitmap here since it gets completely overwritten
3702 * in the merging.
3703 */
3704 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3705 MSR_TYPE_W);
3706 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003707 case MSR_IA32_ARCH_CAPABILITIES:
3708 if (!msr_info->host_initiated)
3709 return 1;
3710 vmx->arch_capabilities = data;
3711 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003712 case MSR_IA32_CR_PAT:
3713 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003714 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3715 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003716 vmcs_write64(GUEST_IA32_PAT, data);
3717 vcpu->arch.pat = data;
3718 break;
3719 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003720 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003721 break;
Will Auldba904632012-11-29 12:42:50 -08003722 case MSR_IA32_TSC_ADJUST:
3723 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003724 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003725 case MSR_IA32_MCG_EXT_CTL:
3726 if ((!msr_info->host_initiated &&
3727 !(to_vmx(vcpu)->msr_ia32_feature_control &
3728 FEATURE_CONTROL_LMCE)) ||
3729 (data & ~MCG_EXT_CTL_LMCE_EN))
3730 return 1;
3731 vcpu->arch.mcg_ext_ctl = data;
3732 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003733 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003734 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003735 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003736 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3737 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003738 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003739 if (msr_info->host_initiated && data == 0)
3740 vmx_leave_nested(vcpu);
3741 break;
3742 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003743 if (!msr_info->host_initiated)
3744 return 1; /* they are read-only */
3745 if (!nested_vmx_allowed(vcpu))
3746 return 1;
3747 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003748 case MSR_IA32_XSS:
3749 if (!vmx_xsaves_supported())
3750 return 1;
3751 /*
3752 * The only supported bit as of Skylake is bit 8, but
3753 * it is not supported on KVM.
3754 */
3755 if (data != 0)
3756 return 1;
3757 vcpu->arch.ia32_xss = data;
3758 if (vcpu->arch.ia32_xss != host_xss)
3759 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3760 vcpu->arch.ia32_xss, host_xss);
3761 else
3762 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3763 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003764 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003765 if (!msr_info->host_initiated &&
3766 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003767 return 1;
3768 /* Check reserved bit, higher 32 bits should be zero */
3769 if ((data >> 32) != 0)
3770 return 1;
3771 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003772 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003773 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003774 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003775 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003776 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003777 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3778 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003779 ret = kvm_set_shared_msr(msr->index, msr->data,
3780 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003781 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003782 if (ret)
3783 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003784 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003785 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003787 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788 }
3789
Eddie Dong2cc51562007-05-21 07:28:09 +03003790 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791}
3792
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003793static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003795 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3796 switch (reg) {
3797 case VCPU_REGS_RSP:
3798 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3799 break;
3800 case VCPU_REGS_RIP:
3801 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3802 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003803 case VCPU_EXREG_PDPTR:
3804 if (enable_ept)
3805 ept_save_pdptrs(vcpu);
3806 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003807 default:
3808 break;
3809 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003810}
3811
Avi Kivity6aa8b732006-12-10 02:21:36 -08003812static __init int cpu_has_kvm_support(void)
3813{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003814 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003815}
3816
3817static __init int vmx_disabled_by_bios(void)
3818{
3819 u64 msr;
3820
3821 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003822 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003823 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003824 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3825 && tboot_enabled())
3826 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003827 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003828 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003829 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003830 && !tboot_enabled()) {
3831 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003832 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003833 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003834 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003835 /* launched w/o TXT and VMX disabled */
3836 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3837 && !tboot_enabled())
3838 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003839 }
3840
3841 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842}
3843
Dongxiao Xu7725b892010-05-11 18:29:38 +08003844static void kvm_cpu_vmxon(u64 addr)
3845{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003846 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003847 intel_pt_handle_vmx(1);
3848
Dongxiao Xu7725b892010-05-11 18:29:38 +08003849 asm volatile (ASM_VMX_VMXON_RAX
3850 : : "a"(&addr), "m"(addr)
3851 : "memory", "cc");
3852}
3853
Radim Krčmář13a34e02014-08-28 15:13:03 +02003854static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855{
3856 int cpu = raw_smp_processor_id();
3857 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003858 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003859
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003860 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003861 return -EBUSY;
3862
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01003863 /*
3864 * This can happen if we hot-added a CPU but failed to allocate
3865 * VP assist page for it.
3866 */
3867 if (static_branch_unlikely(&enable_evmcs) &&
3868 !hv_get_vp_assist_page(cpu))
3869 return -EFAULT;
3870
Nadav Har'Eld462b812011-05-24 15:26:10 +03003871 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003872 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3873 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003874
3875 /*
3876 * Now we can enable the vmclear operation in kdump
3877 * since the loaded_vmcss_on_cpu list on this cpu
3878 * has been initialized.
3879 *
3880 * Though the cpu is not in VMX operation now, there
3881 * is no problem to enable the vmclear operation
3882 * for the loaded_vmcss_on_cpu list is empty!
3883 */
3884 crash_enable_local_vmclear(cpu);
3885
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003887
3888 test_bits = FEATURE_CONTROL_LOCKED;
3889 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3890 if (tboot_enabled())
3891 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3892
3893 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003894 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003895 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3896 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003897 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003898 if (enable_ept)
3899 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003900
3901 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003902}
3903
Nadav Har'Eld462b812011-05-24 15:26:10 +03003904static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003905{
3906 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003907 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003908
Nadav Har'Eld462b812011-05-24 15:26:10 +03003909 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3910 loaded_vmcss_on_cpu_link)
3911 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003912}
3913
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003914
3915/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3916 * tricks.
3917 */
3918static void kvm_cpu_vmxoff(void)
3919{
3920 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003921
3922 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003923 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003924}
3925
Radim Krčmář13a34e02014-08-28 15:13:03 +02003926static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003927{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003928 vmclear_local_loaded_vmcss();
3929 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930}
3931
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003932static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003933 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934{
3935 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003936 u32 ctl = ctl_min | ctl_opt;
3937
3938 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3939
3940 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3941 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3942
3943 /* Ensure minimum (required) set of control bits are supported. */
3944 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003945 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003946
3947 *result = ctl;
3948 return 0;
3949}
3950
Avi Kivity110312c2010-12-21 12:54:20 +02003951static __init bool allow_1_setting(u32 msr, u32 ctl)
3952{
3953 u32 vmx_msr_low, vmx_msr_high;
3954
3955 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3956 return vmx_msr_high & ctl;
3957}
3958
Yang, Sheng002c7f72007-07-31 14:23:01 +03003959static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003960{
3961 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003962 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003963 u32 _pin_based_exec_control = 0;
3964 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003965 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003966 u32 _vmexit_control = 0;
3967 u32 _vmentry_control = 0;
3968
Paolo Bonzini13893092018-02-26 13:40:09 +01003969 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05303970 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003971#ifdef CONFIG_X86_64
3972 CPU_BASED_CR8_LOAD_EXITING |
3973 CPU_BASED_CR8_STORE_EXITING |
3974#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003975 CPU_BASED_CR3_LOAD_EXITING |
3976 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08003977 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003978 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003979 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003980 CPU_BASED_MWAIT_EXITING |
3981 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003982 CPU_BASED_INVLPG_EXITING |
3983 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003984
Sheng Yangf78e0e22007-10-29 09:40:42 +08003985 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003986 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003987 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003988 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3989 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003990 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003991#ifdef CONFIG_X86_64
3992 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3993 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3994 ~CPU_BASED_CR8_STORE_EXITING;
3995#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003996 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003997 min2 = 0;
3998 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003999 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004000 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004001 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004002 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004003 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004004 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004005 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004006 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004007 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004008 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004009 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004010 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004011 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004012 SECONDARY_EXEC_RDSEED_EXITING |
4013 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004014 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004015 SECONDARY_EXEC_TSC_SCALING |
4016 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004017 if (adjust_vmx_controls(min2, opt2,
4018 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004019 &_cpu_based_2nd_exec_control) < 0)
4020 return -EIO;
4021 }
4022#ifndef CONFIG_X86_64
4023 if (!(_cpu_based_2nd_exec_control &
4024 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4025 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4026#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004027
4028 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4029 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004030 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004031 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4032 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004033
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004034 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4035 &vmx_capability.ept, &vmx_capability.vpid);
4036
Sheng Yangd56f5462008-04-25 10:13:16 +08004037 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004038 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4039 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004040 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4041 CPU_BASED_CR3_STORE_EXITING |
4042 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004043 } else if (vmx_capability.ept) {
4044 vmx_capability.ept = 0;
4045 pr_warn_once("EPT CAP should not exist if not support "
4046 "1-setting enable EPT VM-execution control\n");
4047 }
4048 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4049 vmx_capability.vpid) {
4050 vmx_capability.vpid = 0;
4051 pr_warn_once("VPID CAP should not exist if not support "
4052 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004053 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004054
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004055 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004056#ifdef CONFIG_X86_64
4057 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4058#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004059 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004060 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004061 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4062 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004063 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004064
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004065 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4066 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4067 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004068 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4069 &_pin_based_exec_control) < 0)
4070 return -EIO;
4071
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004072 if (cpu_has_broken_vmx_preemption_timer())
4073 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004074 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004075 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004076 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4077
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004078 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004079 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004080 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4081 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004082 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004083
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004084 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004085
4086 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4087 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004088 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004089
4090#ifdef CONFIG_X86_64
4091 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4092 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004093 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004094#endif
4095
4096 /* Require Write-Back (WB) memory type for VMCS accesses. */
4097 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004098 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004099
Yang, Sheng002c7f72007-07-31 14:23:01 +03004100 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004101 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004102 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004103
4104 /* KVM supports Enlightened VMCS v1 only */
4105 if (static_branch_unlikely(&enable_evmcs))
4106 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4107 else
4108 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004109
Yang, Sheng002c7f72007-07-31 14:23:01 +03004110 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4111 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004112 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004113 vmcs_conf->vmexit_ctrl = _vmexit_control;
4114 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004115
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004116 if (static_branch_unlikely(&enable_evmcs))
4117 evmcs_sanitize_exec_ctrls(vmcs_conf);
4118
Avi Kivity110312c2010-12-21 12:54:20 +02004119 cpu_has_load_ia32_efer =
4120 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4121 VM_ENTRY_LOAD_IA32_EFER)
4122 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4123 VM_EXIT_LOAD_IA32_EFER);
4124
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004125 cpu_has_load_perf_global_ctrl =
4126 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4127 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4128 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4129 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4130
4131 /*
4132 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004133 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004134 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4135 *
4136 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4137 *
4138 * AAK155 (model 26)
4139 * AAP115 (model 30)
4140 * AAT100 (model 37)
4141 * BC86,AAY89,BD102 (model 44)
4142 * BA97 (model 46)
4143 *
4144 */
4145 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4146 switch (boot_cpu_data.x86_model) {
4147 case 26:
4148 case 30:
4149 case 37:
4150 case 44:
4151 case 46:
4152 cpu_has_load_perf_global_ctrl = false;
4153 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4154 "does not work properly. Using workaround\n");
4155 break;
4156 default:
4157 break;
4158 }
4159 }
4160
Borislav Petkov782511b2016-04-04 22:25:03 +02004161 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004162 rdmsrl(MSR_IA32_XSS, host_xss);
4163
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004164 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004165}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166
4167static struct vmcs *alloc_vmcs_cpu(int cpu)
4168{
4169 int node = cpu_to_node(cpu);
4170 struct page *pages;
4171 struct vmcs *vmcs;
4172
Vlastimil Babka96db8002015-09-08 15:03:50 -07004173 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 if (!pages)
4175 return NULL;
4176 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004177 memset(vmcs, 0, vmcs_config.size);
4178 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179 return vmcs;
4180}
4181
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182static void free_vmcs(struct vmcs *vmcs)
4183{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004184 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185}
4186
Nadav Har'Eld462b812011-05-24 15:26:10 +03004187/*
4188 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4189 */
4190static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4191{
4192 if (!loaded_vmcs->vmcs)
4193 return;
4194 loaded_vmcs_clear(loaded_vmcs);
4195 free_vmcs(loaded_vmcs->vmcs);
4196 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004197 if (loaded_vmcs->msr_bitmap)
4198 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004199 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004200}
4201
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004202static struct vmcs *alloc_vmcs(void)
4203{
4204 return alloc_vmcs_cpu(raw_smp_processor_id());
4205}
4206
4207static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4208{
4209 loaded_vmcs->vmcs = alloc_vmcs();
4210 if (!loaded_vmcs->vmcs)
4211 return -ENOMEM;
4212
4213 loaded_vmcs->shadow_vmcs = NULL;
4214 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004215
4216 if (cpu_has_vmx_msr_bitmap()) {
4217 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4218 if (!loaded_vmcs->msr_bitmap)
4219 goto out_vmcs;
4220 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4221 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004222 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004223
4224out_vmcs:
4225 free_loaded_vmcs(loaded_vmcs);
4226 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004227}
4228
Sam Ravnborg39959582007-06-01 00:47:13 -07004229static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230{
4231 int cpu;
4232
Zachary Amsden3230bb42009-09-29 11:38:37 -10004233 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004235 per_cpu(vmxarea, cpu) = NULL;
4236 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237}
4238
Jim Mattsond37f4262017-12-22 12:12:16 -08004239enum vmcs_field_width {
4240 VMCS_FIELD_WIDTH_U16 = 0,
4241 VMCS_FIELD_WIDTH_U64 = 1,
4242 VMCS_FIELD_WIDTH_U32 = 2,
4243 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004244};
4245
Jim Mattsond37f4262017-12-22 12:12:16 -08004246static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004247{
4248 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004249 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004250 return (field >> 13) & 0x3 ;
4251}
4252
4253static inline int vmcs_field_readonly(unsigned long field)
4254{
4255 return (((field >> 10) & 0x3) == 1);
4256}
4257
Bandan Dasfe2b2012014-04-21 15:20:14 -04004258static void init_vmcs_shadow_fields(void)
4259{
4260 int i, j;
4261
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004262 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4263 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004264 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004265 (i + 1 == max_shadow_read_only_fields ||
4266 shadow_read_only_fields[i + 1] != field + 1))
4267 pr_err("Missing field from shadow_read_only_field %x\n",
4268 field + 1);
4269
4270 clear_bit(field, vmx_vmread_bitmap);
4271#ifdef CONFIG_X86_64
4272 if (field & 1)
4273 continue;
4274#endif
4275 if (j < i)
4276 shadow_read_only_fields[j] = field;
4277 j++;
4278 }
4279 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004280
4281 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004282 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004283 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004284 (i + 1 == max_shadow_read_write_fields ||
4285 shadow_read_write_fields[i + 1] != field + 1))
4286 pr_err("Missing field from shadow_read_write_field %x\n",
4287 field + 1);
4288
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004289 /*
4290 * PML and the preemption timer can be emulated, but the
4291 * processor cannot vmwrite to fields that don't exist
4292 * on bare metal.
4293 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004294 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004295 case GUEST_PML_INDEX:
4296 if (!cpu_has_vmx_pml())
4297 continue;
4298 break;
4299 case VMX_PREEMPTION_TIMER_VALUE:
4300 if (!cpu_has_vmx_preemption_timer())
4301 continue;
4302 break;
4303 case GUEST_INTR_STATUS:
4304 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004305 continue;
4306 break;
4307 default:
4308 break;
4309 }
4310
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004311 clear_bit(field, vmx_vmwrite_bitmap);
4312 clear_bit(field, vmx_vmread_bitmap);
4313#ifdef CONFIG_X86_64
4314 if (field & 1)
4315 continue;
4316#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004317 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004318 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004319 j++;
4320 }
4321 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004322}
4323
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324static __init int alloc_kvm_area(void)
4325{
4326 int cpu;
4327
Zachary Amsden3230bb42009-09-29 11:38:37 -10004328 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329 struct vmcs *vmcs;
4330
4331 vmcs = alloc_vmcs_cpu(cpu);
4332 if (!vmcs) {
4333 free_kvm_area();
4334 return -ENOMEM;
4335 }
4336
4337 per_cpu(vmxarea, cpu) = vmcs;
4338 }
4339 return 0;
4340}
4341
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004342static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004343 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004344{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004345 if (!emulate_invalid_guest_state) {
4346 /*
4347 * CS and SS RPL should be equal during guest entry according
4348 * to VMX spec, but in reality it is not always so. Since vcpu
4349 * is in the middle of the transition from real mode to
4350 * protected mode it is safe to assume that RPL 0 is a good
4351 * default value.
4352 */
4353 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004354 save->selector &= ~SEGMENT_RPL_MASK;
4355 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004356 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004358 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359}
4360
4361static void enter_pmode(struct kvm_vcpu *vcpu)
4362{
4363 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004364 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365
Gleb Natapovd99e4152012-12-20 16:57:45 +02004366 /*
4367 * Update real mode segment cache. It may be not up-to-date if sement
4368 * register was written while vcpu was in a guest mode.
4369 */
4370 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4371 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4372 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4373 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4374 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4375 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4376
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004377 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378
Avi Kivity2fb92db2011-04-27 19:42:18 +03004379 vmx_segment_cache_clear(vmx);
4380
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004381 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004382
4383 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004384 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4385 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386 vmcs_writel(GUEST_RFLAGS, flags);
4387
Rusty Russell66aee912007-07-17 23:34:16 +10004388 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4389 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004390
4391 update_exception_bitmap(vcpu);
4392
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004393 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4394 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4395 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4396 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4397 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4398 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399}
4400
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004401static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004402{
Mathias Krause772e0312012-08-30 01:30:19 +02004403 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004404 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004405
Gleb Natapovd99e4152012-12-20 16:57:45 +02004406 var.dpl = 0x3;
4407 if (seg == VCPU_SREG_CS)
4408 var.type = 0x3;
4409
4410 if (!emulate_invalid_guest_state) {
4411 var.selector = var.base >> 4;
4412 var.base = var.base & 0xffff0;
4413 var.limit = 0xffff;
4414 var.g = 0;
4415 var.db = 0;
4416 var.present = 1;
4417 var.s = 1;
4418 var.l = 0;
4419 var.unusable = 0;
4420 var.type = 0x3;
4421 var.avl = 0;
4422 if (save->base & 0xf)
4423 printk_once(KERN_WARNING "kvm: segment base is not "
4424 "paragraph aligned when entering "
4425 "protected mode (seg=%d)", seg);
4426 }
4427
4428 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004429 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004430 vmcs_write32(sf->limit, var.limit);
4431 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004432}
4433
4434static void enter_rmode(struct kvm_vcpu *vcpu)
4435{
4436 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004437 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004438 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004439
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004440 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4441 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4442 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4443 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4444 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004445 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4446 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004447
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004448 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449
Gleb Natapov776e58e2011-03-13 12:34:27 +02004450 /*
4451 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004452 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004453 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004454 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004455 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4456 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004457
Avi Kivity2fb92db2011-04-27 19:42:18 +03004458 vmx_segment_cache_clear(vmx);
4459
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004460 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004461 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004462 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4463
4464 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004465 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004467 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468
4469 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004470 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471 update_exception_bitmap(vcpu);
4472
Gleb Natapovd99e4152012-12-20 16:57:45 +02004473 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4474 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4475 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4476 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4477 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4478 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004479
Eddie Dong8668a3c2007-10-10 14:26:45 +08004480 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004481}
4482
Amit Shah401d10d2009-02-20 22:53:37 +05304483static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4484{
4485 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004486 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4487
4488 if (!msr)
4489 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304490
Avi Kivity44ea2b12009-09-06 15:55:37 +03004491 /*
4492 * Force kernel_gs_base reloading before EFER changes, as control
4493 * of this msr depends on is_long_mode().
4494 */
4495 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004496 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304497 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004498 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304499 msr->data = efer;
4500 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004501 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304502
4503 msr->data = efer & ~EFER_LME;
4504 }
4505 setup_msrs(vmx);
4506}
4507
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004508#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004509
4510static void enter_lmode(struct kvm_vcpu *vcpu)
4511{
4512 u32 guest_tr_ar;
4513
Avi Kivity2fb92db2011-04-27 19:42:18 +03004514 vmx_segment_cache_clear(to_vmx(vcpu));
4515
Avi Kivity6aa8b732006-12-10 02:21:36 -08004516 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004517 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004518 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4519 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004520 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004521 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4522 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523 }
Avi Kivityda38f432010-07-06 11:30:49 +03004524 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004525}
4526
4527static void exit_lmode(struct kvm_vcpu *vcpu)
4528{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004529 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004530 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004531}
4532
4533#endif
4534
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004535static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4536 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004537{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004538 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004539 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4540 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004541 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004542 } else {
4543 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004544 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004545}
4546
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004547static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004548{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004549 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004550}
4551
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004552static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4553{
4554 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004555 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004556}
4557
Avi Kivitye8467fd2009-12-29 18:43:06 +02004558static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4559{
4560 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4561
4562 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4563 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4564}
4565
Avi Kivityaff48ba2010-12-05 18:56:11 +02004566static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4567{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004568 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004569 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4570 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4571}
4572
Anthony Liguori25c4c272007-04-27 09:29:21 +03004573static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004574{
Avi Kivityfc78f512009-12-07 12:16:48 +02004575 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4576
4577 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4578 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004579}
4580
Sheng Yang14394422008-04-28 12:24:45 +08004581static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4582{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004583 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4584
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004585 if (!test_bit(VCPU_EXREG_PDPTR,
4586 (unsigned long *)&vcpu->arch.regs_dirty))
4587 return;
4588
Sheng Yang14394422008-04-28 12:24:45 +08004589 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004590 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4591 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4592 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4593 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004594 }
4595}
4596
Avi Kivity8f5d5492009-05-31 18:41:29 +03004597static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4598{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004599 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4600
Avi Kivity8f5d5492009-05-31 18:41:29 +03004601 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004602 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4603 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4604 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4605 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004606 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004607
4608 __set_bit(VCPU_EXREG_PDPTR,
4609 (unsigned long *)&vcpu->arch.regs_avail);
4610 __set_bit(VCPU_EXREG_PDPTR,
4611 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004612}
4613
David Matlack38991522016-11-29 18:14:08 -08004614static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4615{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004616 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4617 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004618 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4619
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004620 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004621 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4622 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4623 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4624
4625 return fixed_bits_valid(val, fixed0, fixed1);
4626}
4627
4628static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4629{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004630 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4631 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004632
4633 return fixed_bits_valid(val, fixed0, fixed1);
4634}
4635
4636static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4637{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004638 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4639 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004640
4641 return fixed_bits_valid(val, fixed0, fixed1);
4642}
4643
4644/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4645#define nested_guest_cr4_valid nested_cr4_valid
4646#define nested_host_cr4_valid nested_cr4_valid
4647
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004648static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004649
4650static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4651 unsigned long cr0,
4652 struct kvm_vcpu *vcpu)
4653{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004654 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4655 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004656 if (!(cr0 & X86_CR0_PG)) {
4657 /* From paging/starting to nonpaging */
4658 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004659 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004660 (CPU_BASED_CR3_LOAD_EXITING |
4661 CPU_BASED_CR3_STORE_EXITING));
4662 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004663 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004664 } else if (!is_paging(vcpu)) {
4665 /* From nonpaging to paging */
4666 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004667 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004668 ~(CPU_BASED_CR3_LOAD_EXITING |
4669 CPU_BASED_CR3_STORE_EXITING));
4670 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004671 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004672 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004673
4674 if (!(cr0 & X86_CR0_WP))
4675 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004676}
4677
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4679{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004680 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004681 unsigned long hw_cr0;
4682
Gleb Natapov50378782013-02-04 16:00:28 +02004683 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004684 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004685 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004686 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004687 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004688
Gleb Natapov218e7632013-01-21 15:36:45 +02004689 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4690 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004691
Gleb Natapov218e7632013-01-21 15:36:45 +02004692 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4693 enter_rmode(vcpu);
4694 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004695
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004696#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004697 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004698 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004699 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004700 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701 exit_lmode(vcpu);
4702 }
4703#endif
4704
Sean Christophersonb4d18512018-03-05 12:04:40 -08004705 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004706 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4707
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004709 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004710 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004711
4712 /* depends on vcpu->arch.cr0 to be set to a new value */
4713 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004714}
4715
Yu Zhang855feb62017-08-24 20:27:55 +08004716static int get_ept_level(struct kvm_vcpu *vcpu)
4717{
4718 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4719 return 5;
4720 return 4;
4721}
4722
Peter Feiner995f00a2017-06-30 17:26:32 -07004723static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004724{
Yu Zhang855feb62017-08-24 20:27:55 +08004725 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004726
Yu Zhang855feb62017-08-24 20:27:55 +08004727 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004728
Peter Feiner995f00a2017-06-30 17:26:32 -07004729 if (enable_ept_ad_bits &&
4730 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004731 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004732 eptp |= (root_hpa & PAGE_MASK);
4733
4734 return eptp;
4735}
4736
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4738{
Sheng Yang14394422008-04-28 12:24:45 +08004739 unsigned long guest_cr3;
4740 u64 eptp;
4741
4742 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004743 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004744 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004745 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004746 if (enable_unrestricted_guest || is_paging(vcpu) ||
4747 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004748 guest_cr3 = kvm_read_cr3(vcpu);
4749 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004750 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004751 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004752 }
4753
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004754 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004755 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756}
4757
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004758static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004759{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004760 /*
4761 * Pass through host's Machine Check Enable value to hw_cr4, which
4762 * is in force while we are in guest mode. Do not let guests control
4763 * this bit, even if host CR4.MCE == 0.
4764 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004765 unsigned long hw_cr4;
4766
4767 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4768 if (enable_unrestricted_guest)
4769 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4770 else if (to_vmx(vcpu)->rmode.vm86_active)
4771 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4772 else
4773 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004774
Paolo Bonzini0367f202016-07-12 10:44:55 +02004775 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4776 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4777 SECONDARY_EXEC_DESC);
4778 hw_cr4 &= ~X86_CR4_UMIP;
Radim Krčmář99158242018-01-31 18:12:50 +01004779 } else if (!is_guest_mode(vcpu) ||
4780 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
Paolo Bonzini0367f202016-07-12 10:44:55 +02004781 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4782 SECONDARY_EXEC_DESC);
4783
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004784 if (cr4 & X86_CR4_VMXE) {
4785 /*
4786 * To use VMXON (and later other VMX instructions), a guest
4787 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4788 * So basically the check on whether to allow nested VMX
4789 * is here.
4790 */
4791 if (!nested_vmx_allowed(vcpu))
4792 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004793 }
David Matlack38991522016-11-29 18:14:08 -08004794
4795 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004796 return 1;
4797
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004798 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004799
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004800 if (!enable_unrestricted_guest) {
4801 if (enable_ept) {
4802 if (!is_paging(vcpu)) {
4803 hw_cr4 &= ~X86_CR4_PAE;
4804 hw_cr4 |= X86_CR4_PSE;
4805 } else if (!(cr4 & X86_CR4_PAE)) {
4806 hw_cr4 &= ~X86_CR4_PAE;
4807 }
4808 }
4809
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004810 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004811 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4812 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4813 * to be manually disabled when guest switches to non-paging
4814 * mode.
4815 *
4816 * If !enable_unrestricted_guest, the CPU is always running
4817 * with CR0.PG=1 and CR4 needs to be modified.
4818 * If enable_unrestricted_guest, the CPU automatically
4819 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004820 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004821 if (!is_paging(vcpu))
4822 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4823 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004824
Sheng Yang14394422008-04-28 12:24:45 +08004825 vmcs_writel(CR4_READ_SHADOW, cr4);
4826 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004827 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828}
4829
Avi Kivity6aa8b732006-12-10 02:21:36 -08004830static void vmx_get_segment(struct kvm_vcpu *vcpu,
4831 struct kvm_segment *var, int seg)
4832{
Avi Kivitya9179492011-01-03 14:28:52 +02004833 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004834 u32 ar;
4835
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004836 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004837 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004838 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004839 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004840 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004841 var->base = vmx_read_guest_seg_base(vmx, seg);
4842 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4843 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004844 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004845 var->base = vmx_read_guest_seg_base(vmx, seg);
4846 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4847 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4848 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004849 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 var->type = ar & 15;
4851 var->s = (ar >> 4) & 1;
4852 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004853 /*
4854 * Some userspaces do not preserve unusable property. Since usable
4855 * segment has to be present according to VMX spec we can use present
4856 * property to amend userspace bug by making unusable segment always
4857 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4858 * segment as unusable.
4859 */
4860 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004861 var->avl = (ar >> 12) & 1;
4862 var->l = (ar >> 13) & 1;
4863 var->db = (ar >> 14) & 1;
4864 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865}
4866
Avi Kivitya9179492011-01-03 14:28:52 +02004867static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4868{
Avi Kivitya9179492011-01-03 14:28:52 +02004869 struct kvm_segment s;
4870
4871 if (to_vmx(vcpu)->rmode.vm86_active) {
4872 vmx_get_segment(vcpu, &s, seg);
4873 return s.base;
4874 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004875 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004876}
4877
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004878static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004879{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004880 struct vcpu_vmx *vmx = to_vmx(vcpu);
4881
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004882 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004883 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004884 else {
4885 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004886 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004887 }
Avi Kivity69c73022011-03-07 15:26:44 +02004888}
4889
Avi Kivity653e3102007-05-07 10:55:37 +03004890static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004892 u32 ar;
4893
Avi Kivityf0495f92012-06-07 17:06:10 +03004894 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004895 ar = 1 << 16;
4896 else {
4897 ar = var->type & 15;
4898 ar |= (var->s & 1) << 4;
4899 ar |= (var->dpl & 3) << 5;
4900 ar |= (var->present & 1) << 7;
4901 ar |= (var->avl & 1) << 12;
4902 ar |= (var->l & 1) << 13;
4903 ar |= (var->db & 1) << 14;
4904 ar |= (var->g & 1) << 15;
4905 }
Avi Kivity653e3102007-05-07 10:55:37 +03004906
4907 return ar;
4908}
4909
4910static void vmx_set_segment(struct kvm_vcpu *vcpu,
4911 struct kvm_segment *var, int seg)
4912{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004913 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004914 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004915
Avi Kivity2fb92db2011-04-27 19:42:18 +03004916 vmx_segment_cache_clear(vmx);
4917
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004918 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4919 vmx->rmode.segs[seg] = *var;
4920 if (seg == VCPU_SREG_TR)
4921 vmcs_write16(sf->selector, var->selector);
4922 else if (var->s)
4923 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004924 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004925 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004926
Avi Kivity653e3102007-05-07 10:55:37 +03004927 vmcs_writel(sf->base, var->base);
4928 vmcs_write32(sf->limit, var->limit);
4929 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004930
4931 /*
4932 * Fix the "Accessed" bit in AR field of segment registers for older
4933 * qemu binaries.
4934 * IA32 arch specifies that at the time of processor reset the
4935 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004936 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004937 * state vmexit when "unrestricted guest" mode is turned on.
4938 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4939 * tree. Newer qemu binaries with that qemu fix would not need this
4940 * kvm hack.
4941 */
4942 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004943 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004944
Gleb Natapovf924d662012-12-12 19:10:55 +02004945 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004946
4947out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004948 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004949}
4950
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4952{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004953 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004954
4955 *db = (ar >> 14) & 1;
4956 *l = (ar >> 13) & 1;
4957}
4958
Gleb Natapov89a27f42010-02-16 10:51:48 +02004959static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004960{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004961 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4962 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963}
4964
Gleb Natapov89a27f42010-02-16 10:51:48 +02004965static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004966{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004967 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4968 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969}
4970
Gleb Natapov89a27f42010-02-16 10:51:48 +02004971static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004972{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004973 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4974 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975}
4976
Gleb Natapov89a27f42010-02-16 10:51:48 +02004977static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004979 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4980 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004981}
4982
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004983static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4984{
4985 struct kvm_segment var;
4986 u32 ar;
4987
4988 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004989 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004990 if (seg == VCPU_SREG_CS)
4991 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004992 ar = vmx_segment_access_rights(&var);
4993
4994 if (var.base != (var.selector << 4))
4995 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004996 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004997 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004998 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004999 return false;
5000
5001 return true;
5002}
5003
5004static bool code_segment_valid(struct kvm_vcpu *vcpu)
5005{
5006 struct kvm_segment cs;
5007 unsigned int cs_rpl;
5008
5009 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005010 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005011
Avi Kivity1872a3f2009-01-04 23:26:52 +02005012 if (cs.unusable)
5013 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005014 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005015 return false;
5016 if (!cs.s)
5017 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005018 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005019 if (cs.dpl > cs_rpl)
5020 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005021 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005022 if (cs.dpl != cs_rpl)
5023 return false;
5024 }
5025 if (!cs.present)
5026 return false;
5027
5028 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5029 return true;
5030}
5031
5032static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5033{
5034 struct kvm_segment ss;
5035 unsigned int ss_rpl;
5036
5037 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005038 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005039
Avi Kivity1872a3f2009-01-04 23:26:52 +02005040 if (ss.unusable)
5041 return true;
5042 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005043 return false;
5044 if (!ss.s)
5045 return false;
5046 if (ss.dpl != ss_rpl) /* DPL != RPL */
5047 return false;
5048 if (!ss.present)
5049 return false;
5050
5051 return true;
5052}
5053
5054static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5055{
5056 struct kvm_segment var;
5057 unsigned int rpl;
5058
5059 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005060 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005061
Avi Kivity1872a3f2009-01-04 23:26:52 +02005062 if (var.unusable)
5063 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005064 if (!var.s)
5065 return false;
5066 if (!var.present)
5067 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005068 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005069 if (var.dpl < rpl) /* DPL < RPL */
5070 return false;
5071 }
5072
5073 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5074 * rights flags
5075 */
5076 return true;
5077}
5078
5079static bool tr_valid(struct kvm_vcpu *vcpu)
5080{
5081 struct kvm_segment tr;
5082
5083 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5084
Avi Kivity1872a3f2009-01-04 23:26:52 +02005085 if (tr.unusable)
5086 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005087 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005088 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005089 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005090 return false;
5091 if (!tr.present)
5092 return false;
5093
5094 return true;
5095}
5096
5097static bool ldtr_valid(struct kvm_vcpu *vcpu)
5098{
5099 struct kvm_segment ldtr;
5100
5101 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5102
Avi Kivity1872a3f2009-01-04 23:26:52 +02005103 if (ldtr.unusable)
5104 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005105 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005106 return false;
5107 if (ldtr.type != 2)
5108 return false;
5109 if (!ldtr.present)
5110 return false;
5111
5112 return true;
5113}
5114
5115static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5116{
5117 struct kvm_segment cs, ss;
5118
5119 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5120 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5121
Nadav Amitb32a9912015-03-29 16:33:04 +03005122 return ((cs.selector & SEGMENT_RPL_MASK) ==
5123 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005124}
5125
5126/*
5127 * Check if guest state is valid. Returns true if valid, false if
5128 * not.
5129 * We assume that registers are always usable
5130 */
5131static bool guest_state_valid(struct kvm_vcpu *vcpu)
5132{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005133 if (enable_unrestricted_guest)
5134 return true;
5135
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005136 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005137 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005138 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5139 return false;
5140 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5141 return false;
5142 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5143 return false;
5144 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5145 return false;
5146 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5147 return false;
5148 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5149 return false;
5150 } else {
5151 /* protected mode guest state checks */
5152 if (!cs_ss_rpl_check(vcpu))
5153 return false;
5154 if (!code_segment_valid(vcpu))
5155 return false;
5156 if (!stack_segment_valid(vcpu))
5157 return false;
5158 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5159 return false;
5160 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5161 return false;
5162 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5163 return false;
5164 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5165 return false;
5166 if (!tr_valid(vcpu))
5167 return false;
5168 if (!ldtr_valid(vcpu))
5169 return false;
5170 }
5171 /* TODO:
5172 * - Add checks on RIP
5173 * - Add checks on RFLAGS
5174 */
5175
5176 return true;
5177}
5178
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005179static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5180{
5181 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5182}
5183
Mike Dayd77c26f2007-10-08 09:02:08 -04005184static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005185{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005186 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005187 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005188 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005189
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005190 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005191 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005192 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5193 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005194 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005195 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005196 r = kvm_write_guest_page(kvm, fn++, &data,
5197 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005198 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005199 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005200 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5201 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005202 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005203 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5204 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005205 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005206 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005207 r = kvm_write_guest_page(kvm, fn, &data,
5208 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5209 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005210out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005211 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005212 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005213}
5214
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005215static int init_rmode_identity_map(struct kvm *kvm)
5216{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005217 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005218 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005219 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005220 u32 tmp;
5221
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005222 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005223 mutex_lock(&kvm->slots_lock);
5224
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005225 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005226 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005227
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005228 if (!kvm_vmx->ept_identity_map_addr)
5229 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5230 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005231
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005232 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005233 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005234 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005235 goto out2;
5236
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005237 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005238 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5239 if (r < 0)
5240 goto out;
5241 /* Set up identity-mapping pagetable for EPT in real mode */
5242 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5243 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5244 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5245 r = kvm_write_guest_page(kvm, identity_map_pfn,
5246 &tmp, i * sizeof(tmp), sizeof(tmp));
5247 if (r < 0)
5248 goto out;
5249 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005250 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005251
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005252out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005253 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005254
5255out2:
5256 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005257 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005258}
5259
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260static void seg_setup(int seg)
5261{
Mathias Krause772e0312012-08-30 01:30:19 +02005262 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005263 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005264
5265 vmcs_write16(sf->selector, 0);
5266 vmcs_writel(sf->base, 0);
5267 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005268 ar = 0x93;
5269 if (seg == VCPU_SREG_CS)
5270 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005271
5272 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005273}
5274
Sheng Yangf78e0e22007-10-29 09:40:42 +08005275static int alloc_apic_access_page(struct kvm *kvm)
5276{
Xiao Guangrong44841412012-09-07 14:14:20 +08005277 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005278 int r = 0;
5279
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005280 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005281 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005282 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005283 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5284 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005285 if (r)
5286 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005287
Tang Chen73a6d942014-09-11 13:38:00 +08005288 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005289 if (is_error_page(page)) {
5290 r = -EFAULT;
5291 goto out;
5292 }
5293
Tang Chenc24ae0d2014-09-24 15:57:58 +08005294 /*
5295 * Do not pin the page in memory, so that memory hot-unplug
5296 * is able to migrate it.
5297 */
5298 put_page(page);
5299 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005300out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005301 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005302 return r;
5303}
5304
Wanpeng Li991e7a02015-09-16 17:30:05 +08005305static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005306{
5307 int vpid;
5308
Avi Kivity919818a2009-03-23 18:01:29 +02005309 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005310 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005311 spin_lock(&vmx_vpid_lock);
5312 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005313 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005314 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005315 else
5316 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005317 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005318 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005319}
5320
Wanpeng Li991e7a02015-09-16 17:30:05 +08005321static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005322{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005323 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005324 return;
5325 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005326 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005327 spin_unlock(&vmx_vpid_lock);
5328}
5329
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005330static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5331 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005332{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005333 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005334
5335 if (!cpu_has_vmx_msr_bitmap())
5336 return;
5337
5338 /*
5339 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5340 * have the write-low and read-high bitmap offsets the wrong way round.
5341 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5342 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005343 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005344 if (type & MSR_TYPE_R)
5345 /* read-low */
5346 __clear_bit(msr, msr_bitmap + 0x000 / f);
5347
5348 if (type & MSR_TYPE_W)
5349 /* write-low */
5350 __clear_bit(msr, msr_bitmap + 0x800 / f);
5351
Sheng Yang25c5f222008-03-28 13:18:56 +08005352 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5353 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005354 if (type & MSR_TYPE_R)
5355 /* read-high */
5356 __clear_bit(msr, msr_bitmap + 0x400 / f);
5357
5358 if (type & MSR_TYPE_W)
5359 /* write-high */
5360 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5361
5362 }
5363}
5364
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005365static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5366 u32 msr, int type)
5367{
5368 int f = sizeof(unsigned long);
5369
5370 if (!cpu_has_vmx_msr_bitmap())
5371 return;
5372
5373 /*
5374 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5375 * have the write-low and read-high bitmap offsets the wrong way round.
5376 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5377 */
5378 if (msr <= 0x1fff) {
5379 if (type & MSR_TYPE_R)
5380 /* read-low */
5381 __set_bit(msr, msr_bitmap + 0x000 / f);
5382
5383 if (type & MSR_TYPE_W)
5384 /* write-low */
5385 __set_bit(msr, msr_bitmap + 0x800 / f);
5386
5387 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5388 msr &= 0x1fff;
5389 if (type & MSR_TYPE_R)
5390 /* read-high */
5391 __set_bit(msr, msr_bitmap + 0x400 / f);
5392
5393 if (type & MSR_TYPE_W)
5394 /* write-high */
5395 __set_bit(msr, msr_bitmap + 0xc00 / f);
5396
5397 }
5398}
5399
5400static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5401 u32 msr, int type, bool value)
5402{
5403 if (value)
5404 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5405 else
5406 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5407}
5408
Wincy Vanf2b93282015-02-03 23:56:03 +08005409/*
5410 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5411 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5412 */
5413static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5414 unsigned long *msr_bitmap_nested,
5415 u32 msr, int type)
5416{
5417 int f = sizeof(unsigned long);
5418
Wincy Vanf2b93282015-02-03 23:56:03 +08005419 /*
5420 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5421 * have the write-low and read-high bitmap offsets the wrong way round.
5422 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5423 */
5424 if (msr <= 0x1fff) {
5425 if (type & MSR_TYPE_R &&
5426 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5427 /* read-low */
5428 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5429
5430 if (type & MSR_TYPE_W &&
5431 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5432 /* write-low */
5433 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5434
5435 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5436 msr &= 0x1fff;
5437 if (type & MSR_TYPE_R &&
5438 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5439 /* read-high */
5440 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5441
5442 if (type & MSR_TYPE_W &&
5443 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5444 /* write-high */
5445 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5446
5447 }
5448}
5449
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005450static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005451{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005452 u8 mode = 0;
5453
5454 if (cpu_has_secondary_exec_ctrls() &&
5455 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5456 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5457 mode |= MSR_BITMAP_MODE_X2APIC;
5458 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5459 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5460 }
5461
5462 if (is_long_mode(vcpu))
5463 mode |= MSR_BITMAP_MODE_LM;
5464
5465 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005466}
5467
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005468#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5469
5470static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5471 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005472{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005473 int msr;
5474
5475 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5476 unsigned word = msr / BITS_PER_LONG;
5477 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5478 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005479 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005480
5481 if (mode & MSR_BITMAP_MODE_X2APIC) {
5482 /*
5483 * TPR reads and writes can be virtualized even if virtual interrupt
5484 * delivery is not in use.
5485 */
5486 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5487 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5488 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5489 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5490 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5491 }
5492 }
5493}
5494
5495static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5496{
5497 struct vcpu_vmx *vmx = to_vmx(vcpu);
5498 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5499 u8 mode = vmx_msr_bitmap_mode(vcpu);
5500 u8 changed = mode ^ vmx->msr_bitmap_mode;
5501
5502 if (!changed)
5503 return;
5504
5505 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5506 !(mode & MSR_BITMAP_MODE_LM));
5507
5508 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5509 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5510
5511 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005512}
5513
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005514static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005515{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005516 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005517}
5518
David Matlackc9f04402017-08-01 14:00:40 -07005519static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5520{
5521 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5522 gfn_t gfn;
5523
5524 /*
5525 * Don't need to mark the APIC access page dirty; it is never
5526 * written to by the CPU during APIC virtualization.
5527 */
5528
5529 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5530 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5531 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5532 }
5533
5534 if (nested_cpu_has_posted_intr(vmcs12)) {
5535 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5536 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5537 }
5538}
5539
5540
David Hildenbrand6342c502017-01-25 11:58:58 +01005541static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005542{
5543 struct vcpu_vmx *vmx = to_vmx(vcpu);
5544 int max_irr;
5545 void *vapic_page;
5546 u16 status;
5547
David Matlackc9f04402017-08-01 14:00:40 -07005548 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5549 return;
Wincy Van705699a2015-02-03 23:58:17 +08005550
David Matlackc9f04402017-08-01 14:00:40 -07005551 vmx->nested.pi_pending = false;
5552 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5553 return;
Wincy Van705699a2015-02-03 23:58:17 +08005554
David Matlackc9f04402017-08-01 14:00:40 -07005555 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5556 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005557 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005558 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5559 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005560 kunmap(vmx->nested.virtual_apic_page);
5561
5562 status = vmcs_read16(GUEST_INTR_STATUS);
5563 if ((u8)max_irr > ((u8)status & 0xff)) {
5564 status &= ~0xff;
5565 status |= (u8)max_irr;
5566 vmcs_write16(GUEST_INTR_STATUS, status);
5567 }
5568 }
David Matlackc9f04402017-08-01 14:00:40 -07005569
5570 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005571}
5572
Wincy Van06a55242017-04-28 13:13:59 +08005573static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5574 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005575{
5576#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005577 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5578
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005579 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005580 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005581 * The vector of interrupt to be delivered to vcpu had
5582 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005583 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005584 * Following cases will be reached in this block, and
5585 * we always send a notification event in all cases as
5586 * explained below.
5587 *
5588 * Case 1: vcpu keeps in non-root mode. Sending a
5589 * notification event posts the interrupt to vcpu.
5590 *
5591 * Case 2: vcpu exits to root mode and is still
5592 * runnable. PIR will be synced to vIRR before the
5593 * next vcpu entry. Sending a notification event in
5594 * this case has no effect, as vcpu is not in root
5595 * mode.
5596 *
5597 * Case 3: vcpu exits to root mode and is blocked.
5598 * vcpu_block() has already synced PIR to vIRR and
5599 * never blocks vcpu if vIRR is not cleared. Therefore,
5600 * a blocked vcpu here does not wait for any requested
5601 * interrupts in PIR, and sending a notification event
5602 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005603 */
Feng Wu28b835d2015-09-18 22:29:54 +08005604
Wincy Van06a55242017-04-28 13:13:59 +08005605 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005606 return true;
5607 }
5608#endif
5609 return false;
5610}
5611
Wincy Van705699a2015-02-03 23:58:17 +08005612static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5613 int vector)
5614{
5615 struct vcpu_vmx *vmx = to_vmx(vcpu);
5616
5617 if (is_guest_mode(vcpu) &&
5618 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005619 /*
5620 * If a posted intr is not recognized by hardware,
5621 * we will accomplish it in the next vmentry.
5622 */
5623 vmx->nested.pi_pending = true;
5624 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005625 /* the PIR and ON have been set by L1. */
5626 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5627 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005628 return 0;
5629 }
5630 return -1;
5631}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005632/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005633 * Send interrupt to vcpu via posted interrupt way.
5634 * 1. If target vcpu is running(non-root mode), send posted interrupt
5635 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5636 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5637 * interrupt from PIR in next vmentry.
5638 */
5639static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5640{
5641 struct vcpu_vmx *vmx = to_vmx(vcpu);
5642 int r;
5643
Wincy Van705699a2015-02-03 23:58:17 +08005644 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5645 if (!r)
5646 return;
5647
Yang Zhanga20ed542013-04-11 19:25:15 +08005648 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5649 return;
5650
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005651 /* If a previous notification has sent the IPI, nothing to do. */
5652 if (pi_test_and_set_on(&vmx->pi_desc))
5653 return;
5654
Wincy Van06a55242017-04-28 13:13:59 +08005655 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005656 kvm_vcpu_kick(vcpu);
5657}
5658
Avi Kivity6aa8b732006-12-10 02:21:36 -08005659/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005660 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5661 * will not change in the lifetime of the guest.
5662 * Note that host-state that does change is set elsewhere. E.g., host-state
5663 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5664 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005665static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005666{
5667 u32 low32, high32;
5668 unsigned long tmpl;
5669 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005670 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005671
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005672 cr0 = read_cr0();
5673 WARN_ON(cr0 & X86_CR0_TS);
5674 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005675
5676 /*
5677 * Save the most likely value for this task's CR3 in the VMCS.
5678 * We can't use __get_current_cr3_fast() because we're not atomic.
5679 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005680 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005681 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005682 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005683
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005684 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005685 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005686 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005687 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005688
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005689 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005690#ifdef CONFIG_X86_64
5691 /*
5692 * Load null selectors, so we can avoid reloading them in
5693 * __vmx_load_host_state(), in case userspace uses the null selectors
5694 * too (the expected case).
5695 */
5696 vmcs_write16(HOST_DS_SELECTOR, 0);
5697 vmcs_write16(HOST_ES_SELECTOR, 0);
5698#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005699 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5700 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005701#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005702 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5703 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5704
Juergen Gross87930012017-09-04 12:25:27 +02005705 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005706 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005707 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005708
Avi Kivity83287ea422012-09-16 15:10:57 +03005709 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005710
5711 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5712 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5713 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5714 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5715
5716 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5717 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5718 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5719 }
5720}
5721
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005722static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5723{
5724 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5725 if (enable_ept)
5726 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005727 if (is_guest_mode(&vmx->vcpu))
5728 vmx->vcpu.arch.cr4_guest_owned_bits &=
5729 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005730 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5731}
5732
Yang Zhang01e439b2013-04-11 19:25:12 +08005733static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5734{
5735 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5736
Andrey Smetanind62caab2015-11-10 15:36:33 +03005737 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005738 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005739
5740 if (!enable_vnmi)
5741 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5742
Yunhong Jiang64672c92016-06-13 14:19:59 -07005743 /* Enable the preemption timer dynamically */
5744 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005745 return pin_based_exec_ctrl;
5746}
5747
Andrey Smetanind62caab2015-11-10 15:36:33 +03005748static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5749{
5750 struct vcpu_vmx *vmx = to_vmx(vcpu);
5751
5752 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005753 if (cpu_has_secondary_exec_ctrls()) {
5754 if (kvm_vcpu_apicv_active(vcpu))
5755 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5756 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5757 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5758 else
5759 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5760 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5761 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5762 }
5763
5764 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005765 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005766}
5767
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005768static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5769{
5770 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005771
5772 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5773 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5774
Paolo Bonzini35754c92015-07-29 12:05:37 +02005775 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005776 exec_control &= ~CPU_BASED_TPR_SHADOW;
5777#ifdef CONFIG_X86_64
5778 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5779 CPU_BASED_CR8_LOAD_EXITING;
5780#endif
5781 }
5782 if (!enable_ept)
5783 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5784 CPU_BASED_CR3_LOAD_EXITING |
5785 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005786 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5787 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5788 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005789 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5790 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005791 return exec_control;
5792}
5793
Jim Mattson45ec3682017-08-23 16:32:04 -07005794static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005795{
Jim Mattson45ec3682017-08-23 16:32:04 -07005796 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005797 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005798}
5799
Jim Mattson75f4fc82017-08-23 16:32:03 -07005800static bool vmx_rdseed_supported(void)
5801{
5802 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005803 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005804}
5805
Paolo Bonzini80154d72017-08-24 13:55:35 +02005806static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005807{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005808 struct kvm_vcpu *vcpu = &vmx->vcpu;
5809
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005810 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005811
Paolo Bonzini80154d72017-08-24 13:55:35 +02005812 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005813 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5814 if (vmx->vpid == 0)
5815 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5816 if (!enable_ept) {
5817 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5818 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005819 /* Enable INVPCID for non-ept guests may cause performance regression. */
5820 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005821 }
5822 if (!enable_unrestricted_guest)
5823 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005824 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005825 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005826 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005827 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5828 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005829 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005830
5831 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5832 * in vmx_set_cr4. */
5833 exec_control &= ~SECONDARY_EXEC_DESC;
5834
Abel Gordonabc4fc52013-04-18 14:35:25 +03005835 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5836 (handle_vmptrld).
5837 We can NOT enable shadow_vmcs here because we don't have yet
5838 a current VMCS12
5839 */
5840 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005841
5842 if (!enable_pml)
5843 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005844
Paolo Bonzini3db13482017-08-24 14:48:03 +02005845 if (vmx_xsaves_supported()) {
5846 /* Exposing XSAVES only when XSAVE is exposed */
5847 bool xsaves_enabled =
5848 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5849 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5850
5851 if (!xsaves_enabled)
5852 exec_control &= ~SECONDARY_EXEC_XSAVES;
5853
5854 if (nested) {
5855 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005856 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005857 SECONDARY_EXEC_XSAVES;
5858 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005859 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005860 ~SECONDARY_EXEC_XSAVES;
5861 }
5862 }
5863
Paolo Bonzini80154d72017-08-24 13:55:35 +02005864 if (vmx_rdtscp_supported()) {
5865 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5866 if (!rdtscp_enabled)
5867 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5868
5869 if (nested) {
5870 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005871 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005872 SECONDARY_EXEC_RDTSCP;
5873 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005874 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005875 ~SECONDARY_EXEC_RDTSCP;
5876 }
5877 }
5878
5879 if (vmx_invpcid_supported()) {
5880 /* Exposing INVPCID only when PCID is exposed */
5881 bool invpcid_enabled =
5882 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5883 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5884
5885 if (!invpcid_enabled) {
5886 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5887 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5888 }
5889
5890 if (nested) {
5891 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005892 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005893 SECONDARY_EXEC_ENABLE_INVPCID;
5894 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005895 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005896 ~SECONDARY_EXEC_ENABLE_INVPCID;
5897 }
5898 }
5899
Jim Mattson45ec3682017-08-23 16:32:04 -07005900 if (vmx_rdrand_supported()) {
5901 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5902 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005903 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005904
5905 if (nested) {
5906 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005907 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005908 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005909 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005910 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005911 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005912 }
5913 }
5914
Jim Mattson75f4fc82017-08-23 16:32:03 -07005915 if (vmx_rdseed_supported()) {
5916 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5917 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005918 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005919
5920 if (nested) {
5921 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005922 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005923 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005924 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005925 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005926 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005927 }
5928 }
5929
Paolo Bonzini80154d72017-08-24 13:55:35 +02005930 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005931}
5932
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005933static void ept_set_mmio_spte_mask(void)
5934{
5935 /*
5936 * EPT Misconfigurations can be generated if the value of bits 2:0
5937 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005938 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005939 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5940 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005941}
5942
Wanpeng Lif53cd632014-12-02 19:14:58 +08005943#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005944/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005945 * Sets up the vmcs for emulated real mode.
5946 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005947static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005948{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005949#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005950 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005951#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005952 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005953
Abel Gordon4607c2d2013-04-18 14:35:55 +03005954 if (enable_shadow_vmcs) {
5955 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5956 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5957 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005958 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005959 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005960
Avi Kivity6aa8b732006-12-10 02:21:36 -08005961 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5962
Avi Kivity6aa8b732006-12-10 02:21:36 -08005963 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005964 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005965 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005966
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005967 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005968
Dan Williamsdfa169b2016-06-02 11:17:24 -07005969 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005970 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005971 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005972 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005973 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005974
Andrey Smetanind62caab2015-11-10 15:36:33 +03005975 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005976 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5977 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5978 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5979 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5980
5981 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005982
Li RongQing0bcf2612015-12-03 13:29:34 +08005983 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005984 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005985 }
5986
Wanpeng Lib31c1142018-03-12 04:53:04 -07005987 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005988 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005989 vmx->ple_window = ple_window;
5990 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005991 }
5992
Xiao Guangrongc3707952011-07-12 03:28:04 +08005993 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5994 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005995 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5996
Avi Kivity9581d442010-10-19 16:46:55 +02005997 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5998 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005999 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006000#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006001 rdmsrl(MSR_FS_BASE, a);
6002 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6003 rdmsrl(MSR_GS_BASE, a);
6004 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6005#else
6006 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6007 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6008#endif
6009
Bandan Das2a499e42017-08-03 15:54:41 -04006010 if (cpu_has_vmx_vmfunc())
6011 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6012
Eddie Dong2cc51562007-05-21 07:28:09 +03006013 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6014 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006015 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006016 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006017 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006018
Radim Krčmář74545702015-04-27 15:11:25 +02006019 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6020 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006021
Paolo Bonzini03916db2014-07-24 14:21:57 +02006022 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006023 u32 index = vmx_msr_index[i];
6024 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006025 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006026
6027 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6028 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006029 if (wrmsr_safe(index, data_low, data_high) < 0)
6030 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006031 vmx->guest_msrs[j].index = i;
6032 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006033 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006034 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006035 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006036
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006037 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6038 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006039
6040 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006041
6042 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006043 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006044
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006045 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6046 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6047
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006048 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006049
Wanpeng Lif53cd632014-12-02 19:14:58 +08006050 if (vmx_xsaves_supported())
6051 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6052
Peter Feiner4e595162016-07-07 14:49:58 -07006053 if (enable_pml) {
6054 ASSERT(vmx->pml_pg);
6055 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6056 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6057 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006058}
6059
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006060static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006061{
6062 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006063 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006064 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006065
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006066 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006067 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006068
Wanpeng Li518e7b92018-02-28 14:03:31 +08006069 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006070 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006071 kvm_set_cr8(vcpu, 0);
6072
6073 if (!init_event) {
6074 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6075 MSR_IA32_APICBASE_ENABLE;
6076 if (kvm_vcpu_is_reset_bsp(vcpu))
6077 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6078 apic_base_msr.host_initiated = true;
6079 kvm_set_apic_base(vcpu, &apic_base_msr);
6080 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006081
Avi Kivity2fb92db2011-04-27 19:42:18 +03006082 vmx_segment_cache_clear(vmx);
6083
Avi Kivity5706be02008-08-20 15:07:31 +03006084 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006085 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006086 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006087
6088 seg_setup(VCPU_SREG_DS);
6089 seg_setup(VCPU_SREG_ES);
6090 seg_setup(VCPU_SREG_FS);
6091 seg_setup(VCPU_SREG_GS);
6092 seg_setup(VCPU_SREG_SS);
6093
6094 vmcs_write16(GUEST_TR_SELECTOR, 0);
6095 vmcs_writel(GUEST_TR_BASE, 0);
6096 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6097 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6098
6099 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6100 vmcs_writel(GUEST_LDTR_BASE, 0);
6101 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6102 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6103
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006104 if (!init_event) {
6105 vmcs_write32(GUEST_SYSENTER_CS, 0);
6106 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6107 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6108 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6109 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006110
Wanpeng Lic37c2872017-11-20 14:52:21 -08006111 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006112 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006113
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006114 vmcs_writel(GUEST_GDTR_BASE, 0);
6115 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6116
6117 vmcs_writel(GUEST_IDTR_BASE, 0);
6118 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6119
Anthony Liguori443381a2010-12-06 10:53:38 -06006120 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006121 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006122 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006123 if (kvm_mpx_supported())
6124 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006125
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006126 setup_msrs(vmx);
6127
Avi Kivity6aa8b732006-12-10 02:21:36 -08006128 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6129
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006130 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006131 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006132 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006133 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006134 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006135 vmcs_write32(TPR_THRESHOLD, 0);
6136 }
6137
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006138 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006139
Sheng Yang2384d2b2008-01-17 15:14:33 +08006140 if (vmx->vpid != 0)
6141 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6142
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006143 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006144 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006145 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006146 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006147 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006148
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006149 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006150
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006151 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006152 if (init_event)
6153 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006154}
6155
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006156/*
6157 * In nested virtualization, check if L1 asked to exit on external interrupts.
6158 * For most existing hypervisors, this will always return true.
6159 */
6160static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6161{
6162 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6163 PIN_BASED_EXT_INTR_MASK;
6164}
6165
Bandan Das77b0f5d2014-04-19 18:17:45 -04006166/*
6167 * In nested virtualization, check if L1 has set
6168 * VM_EXIT_ACK_INTR_ON_EXIT
6169 */
6170static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6171{
6172 return get_vmcs12(vcpu)->vm_exit_controls &
6173 VM_EXIT_ACK_INTR_ON_EXIT;
6174}
6175
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006176static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6177{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006178 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006179}
6180
Jan Kiszkac9a79532014-03-07 20:03:15 +01006181static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006182{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006183 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6184 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006185}
6186
Jan Kiszkac9a79532014-03-07 20:03:15 +01006187static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006188{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006189 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006190 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006191 enable_irq_window(vcpu);
6192 return;
6193 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006194
Paolo Bonzini47c01522016-12-19 11:44:07 +01006195 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6196 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006197}
6198
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006199static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006200{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006201 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006202 uint32_t intr;
6203 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006204
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006205 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006206
Avi Kivityfa89a812008-09-01 15:57:51 +03006207 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006208 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006209 int inc_eip = 0;
6210 if (vcpu->arch.interrupt.soft)
6211 inc_eip = vcpu->arch.event_exit_inst_len;
6212 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006213 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006214 return;
6215 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006216 intr = irq | INTR_INFO_VALID_MASK;
6217 if (vcpu->arch.interrupt.soft) {
6218 intr |= INTR_TYPE_SOFT_INTR;
6219 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6220 vmx->vcpu.arch.event_exit_inst_len);
6221 } else
6222 intr |= INTR_TYPE_EXT_INTR;
6223 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006224
6225 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006226}
6227
Sheng Yangf08864b2008-05-15 18:23:25 +08006228static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6229{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006230 struct vcpu_vmx *vmx = to_vmx(vcpu);
6231
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006232 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006233 /*
6234 * Tracking the NMI-blocked state in software is built upon
6235 * finding the next open IRQ window. This, in turn, depends on
6236 * well-behaving guests: They have to keep IRQs disabled at
6237 * least as long as the NMI handler runs. Otherwise we may
6238 * cause NMI nesting, maybe breaking the guest. But as this is
6239 * highly unlikely, we can live with the residual risk.
6240 */
6241 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6242 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6243 }
6244
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006245 ++vcpu->stat.nmi_injections;
6246 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006247
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006248 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006249 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006250 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006251 return;
6252 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006253
Sheng Yangf08864b2008-05-15 18:23:25 +08006254 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6255 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006256
6257 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006258}
6259
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006260static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6261{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006262 struct vcpu_vmx *vmx = to_vmx(vcpu);
6263 bool masked;
6264
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006265 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006266 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006267 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006268 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006269 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6270 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6271 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006272}
6273
6274static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6275{
6276 struct vcpu_vmx *vmx = to_vmx(vcpu);
6277
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006278 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006279 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6280 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6281 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6282 }
6283 } else {
6284 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6285 if (masked)
6286 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6287 GUEST_INTR_STATE_NMI);
6288 else
6289 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6290 GUEST_INTR_STATE_NMI);
6291 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006292}
6293
Jan Kiszka2505dc92013-04-14 12:12:47 +02006294static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6295{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006296 if (to_vmx(vcpu)->nested.nested_run_pending)
6297 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006298
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006299 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006300 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6301 return 0;
6302
Jan Kiszka2505dc92013-04-14 12:12:47 +02006303 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6304 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6305 | GUEST_INTR_STATE_NMI));
6306}
6307
Gleb Natapov78646122009-03-23 12:12:11 +02006308static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6309{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006310 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6311 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006312 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6313 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006314}
6315
Izik Eiduscbc94022007-10-25 00:29:55 +02006316static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6317{
6318 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006319
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006320 if (enable_unrestricted_guest)
6321 return 0;
6322
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006323 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6324 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006325 if (ret)
6326 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006327 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006328 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006329}
6330
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006331static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6332{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006333 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006334 return 0;
6335}
6336
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006337static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006338{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006339 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006340 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006341 /*
6342 * Update instruction length as we may reinject the exception
6343 * from user space while in guest debugging mode.
6344 */
6345 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6346 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006347 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006348 return false;
6349 /* fall through */
6350 case DB_VECTOR:
6351 if (vcpu->guest_debug &
6352 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6353 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006354 /* fall through */
6355 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006356 case OF_VECTOR:
6357 case BR_VECTOR:
6358 case UD_VECTOR:
6359 case DF_VECTOR:
6360 case SS_VECTOR:
6361 case GP_VECTOR:
6362 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006363 return true;
6364 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006365 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006366 return false;
6367}
6368
6369static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6370 int vec, u32 err_code)
6371{
6372 /*
6373 * Instruction with address size override prefix opcode 0x67
6374 * Cause the #SS fault with 0 error code in VM86 mode.
6375 */
6376 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6377 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6378 if (vcpu->arch.halt_request) {
6379 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006380 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006381 }
6382 return 1;
6383 }
6384 return 0;
6385 }
6386
6387 /*
6388 * Forward all other exceptions that are valid in real mode.
6389 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6390 * the required debugging infrastructure rework.
6391 */
6392 kvm_queue_exception(vcpu, vec);
6393 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006394}
6395
Andi Kleena0861c02009-06-08 17:37:09 +08006396/*
6397 * Trigger machine check on the host. We assume all the MSRs are already set up
6398 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6399 * We pass a fake environment to the machine check handler because we want
6400 * the guest to be always treated like user space, no matter what context
6401 * it used internally.
6402 */
6403static void kvm_machine_check(void)
6404{
6405#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6406 struct pt_regs regs = {
6407 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6408 .flags = X86_EFLAGS_IF,
6409 };
6410
6411 do_machine_check(&regs, 0);
6412#endif
6413}
6414
Avi Kivity851ba692009-08-24 11:10:17 +03006415static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006416{
6417 /* already handled by vcpu_run */
6418 return 1;
6419}
6420
Avi Kivity851ba692009-08-24 11:10:17 +03006421static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006422{
Avi Kivity1155f762007-11-22 11:30:47 +02006423 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006424 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006425 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006426 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006427 u32 vect_info;
6428 enum emulation_result er;
6429
Avi Kivity1155f762007-11-22 11:30:47 +02006430 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006431 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006432
Andi Kleena0861c02009-06-08 17:37:09 +08006433 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006434 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006435
Jim Mattsonef85b672016-12-12 11:01:37 -08006436 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006437 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006438
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006439 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01006440 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02006441 if (er == EMULATE_USER_EXIT)
6442 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006443 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02006444 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006445 return 1;
6446 }
6447
Avi Kivity6aa8b732006-12-10 02:21:36 -08006448 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006449 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006450 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006451
Liran Alon9e869482018-03-12 13:12:51 +02006452 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6453 WARN_ON_ONCE(!enable_vmware_backdoor);
6454 er = emulate_instruction(vcpu,
6455 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6456 if (er == EMULATE_USER_EXIT)
6457 return 0;
6458 else if (er != EMULATE_DONE)
6459 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6460 return 1;
6461 }
6462
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006463 /*
6464 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6465 * MMIO, it is better to report an internal error.
6466 * See the comments in vmx_handle_exit.
6467 */
6468 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6469 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6470 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6471 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006472 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006473 vcpu->run->internal.data[0] = vect_info;
6474 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006475 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006476 return 0;
6477 }
6478
Avi Kivity6aa8b732006-12-10 02:21:36 -08006479 if (is_page_fault(intr_info)) {
6480 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006481 /* EPT won't cause page fault directly */
6482 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006483 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006484 }
6485
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006486 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006487
6488 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6489 return handle_rmode_exception(vcpu, ex_no, error_code);
6490
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006491 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006492 case AC_VECTOR:
6493 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6494 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006495 case DB_VECTOR:
6496 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6497 if (!(vcpu->guest_debug &
6498 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006499 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006500 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01006501 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
6502 skip_emulated_instruction(vcpu);
6503
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006504 kvm_queue_exception(vcpu, DB_VECTOR);
6505 return 1;
6506 }
6507 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6508 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6509 /* fall through */
6510 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006511 /*
6512 * Update instruction length as we may reinject #BP from
6513 * user space while in guest debugging mode. Reading it for
6514 * #DB as well causes no harm, it is not used in that case.
6515 */
6516 vmx->vcpu.arch.event_exit_inst_len =
6517 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006518 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006519 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006520 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6521 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006522 break;
6523 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006524 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6525 kvm_run->ex.exception = ex_no;
6526 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006527 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006528 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006529 return 0;
6530}
6531
Avi Kivity851ba692009-08-24 11:10:17 +03006532static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006533{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006534 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006535 return 1;
6536}
6537
Avi Kivity851ba692009-08-24 11:10:17 +03006538static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006539{
Avi Kivity851ba692009-08-24 11:10:17 +03006540 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006541 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006542 return 0;
6543}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006544
Avi Kivity851ba692009-08-24 11:10:17 +03006545static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006546{
He, Qingbfdaab02007-09-12 14:18:28 +08006547 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006548 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006549 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006550
He, Qingbfdaab02007-09-12 14:18:28 +08006551 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006552 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006553
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006554 ++vcpu->stat.io_exits;
6555
Sean Christopherson432baf62018-03-08 08:57:26 -08006556 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006557 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006558
6559 port = exit_qualification >> 16;
6560 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006561 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006562
Sean Christophersondca7f122018-03-08 08:57:27 -08006563 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006564}
6565
Ingo Molnar102d8322007-02-19 14:37:47 +02006566static void
6567vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6568{
6569 /*
6570 * Patch in the VMCALL instruction:
6571 */
6572 hypercall[0] = 0x0f;
6573 hypercall[1] = 0x01;
6574 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006575}
6576
Guo Chao0fa06072012-06-28 15:16:19 +08006577/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006578static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6579{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006580 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006581 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6582 unsigned long orig_val = val;
6583
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006584 /*
6585 * We get here when L2 changed cr0 in a way that did not change
6586 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006587 * but did change L0 shadowed bits. So we first calculate the
6588 * effective cr0 value that L1 would like to write into the
6589 * hardware. It consists of the L2-owned bits from the new
6590 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006591 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006592 val = (val & ~vmcs12->cr0_guest_host_mask) |
6593 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6594
David Matlack38991522016-11-29 18:14:08 -08006595 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006596 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006597
6598 if (kvm_set_cr0(vcpu, val))
6599 return 1;
6600 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006601 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006602 } else {
6603 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006604 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006605 return 1;
David Matlack38991522016-11-29 18:14:08 -08006606
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006607 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006608 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006609}
6610
6611static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6612{
6613 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006614 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6615 unsigned long orig_val = val;
6616
6617 /* analogously to handle_set_cr0 */
6618 val = (val & ~vmcs12->cr4_guest_host_mask) |
6619 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6620 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006621 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006622 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006623 return 0;
6624 } else
6625 return kvm_set_cr4(vcpu, val);
6626}
6627
Paolo Bonzini0367f202016-07-12 10:44:55 +02006628static int handle_desc(struct kvm_vcpu *vcpu)
6629{
6630 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6631 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6632}
6633
Avi Kivity851ba692009-08-24 11:10:17 +03006634static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006635{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006636 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006637 int cr;
6638 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006639 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006640 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006641
He, Qingbfdaab02007-09-12 14:18:28 +08006642 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006643 cr = exit_qualification & 15;
6644 reg = (exit_qualification >> 8) & 15;
6645 switch ((exit_qualification >> 4) & 3) {
6646 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006647 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006648 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006649 switch (cr) {
6650 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006651 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006652 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006653 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006654 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006655 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006656 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006657 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006658 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006659 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006660 case 8: {
6661 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006662 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006663 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006664 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006665 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006666 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006667 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006668 return ret;
6669 /*
6670 * TODO: we might be squashing a
6671 * KVM_GUESTDBG_SINGLESTEP-triggered
6672 * KVM_EXIT_DEBUG here.
6673 */
Avi Kivity851ba692009-08-24 11:10:17 +03006674 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006675 return 0;
6676 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006677 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006678 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006679 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006680 WARN_ONCE(1, "Guest should always own CR0.TS");
6681 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006682 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006683 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006684 case 1: /*mov from cr*/
6685 switch (cr) {
6686 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006687 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006688 val = kvm_read_cr3(vcpu);
6689 kvm_register_write(vcpu, reg, val);
6690 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006691 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006692 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006693 val = kvm_get_cr8(vcpu);
6694 kvm_register_write(vcpu, reg, val);
6695 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006696 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006697 }
6698 break;
6699 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006700 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006701 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006702 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006703
Kyle Huey6affcbe2016-11-29 12:40:40 -08006704 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006705 default:
6706 break;
6707 }
Avi Kivity851ba692009-08-24 11:10:17 +03006708 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006709 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006710 (int)(exit_qualification >> 4) & 3, cr);
6711 return 0;
6712}
6713
Avi Kivity851ba692009-08-24 11:10:17 +03006714static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006715{
He, Qingbfdaab02007-09-12 14:18:28 +08006716 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006717 int dr, dr7, reg;
6718
6719 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6720 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6721
6722 /* First, if DR does not exist, trigger UD */
6723 if (!kvm_require_dr(vcpu, dr))
6724 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006725
Jan Kiszkaf2483412010-01-20 18:20:20 +01006726 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006727 if (!kvm_require_cpl(vcpu, 0))
6728 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006729 dr7 = vmcs_readl(GUEST_DR7);
6730 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006731 /*
6732 * As the vm-exit takes precedence over the debug trap, we
6733 * need to emulate the latter, either for the host or the
6734 * guest debugging itself.
6735 */
6736 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006737 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006738 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006739 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006740 vcpu->run->debug.arch.exception = DB_VECTOR;
6741 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006742 return 0;
6743 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006744 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006745 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006746 kvm_queue_exception(vcpu, DB_VECTOR);
6747 return 1;
6748 }
6749 }
6750
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006751 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006752 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6753 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006754
6755 /*
6756 * No more DR vmexits; force a reload of the debug registers
6757 * and reenter on this instruction. The next vmexit will
6758 * retrieve the full state of the debug registers.
6759 */
6760 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6761 return 1;
6762 }
6763
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006764 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6765 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006766 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006767
6768 if (kvm_get_dr(vcpu, dr, &val))
6769 return 1;
6770 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006771 } else
Nadav Amit57773922014-06-18 17:19:23 +03006772 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006773 return 1;
6774
Kyle Huey6affcbe2016-11-29 12:40:40 -08006775 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006776}
6777
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006778static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6779{
6780 return vcpu->arch.dr6;
6781}
6782
6783static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6784{
6785}
6786
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006787static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6788{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006789 get_debugreg(vcpu->arch.db[0], 0);
6790 get_debugreg(vcpu->arch.db[1], 1);
6791 get_debugreg(vcpu->arch.db[2], 2);
6792 get_debugreg(vcpu->arch.db[3], 3);
6793 get_debugreg(vcpu->arch.dr6, 6);
6794 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6795
6796 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006797 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006798}
6799
Gleb Natapov020df072010-04-13 10:05:23 +03006800static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6801{
6802 vmcs_writel(GUEST_DR7, val);
6803}
6804
Avi Kivity851ba692009-08-24 11:10:17 +03006805static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006806{
Kyle Huey6a908b62016-11-29 12:40:37 -08006807 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006808}
6809
Avi Kivity851ba692009-08-24 11:10:17 +03006810static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006811{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006812 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006813 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006814
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006815 msr_info.index = ecx;
6816 msr_info.host_initiated = false;
6817 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006818 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006819 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006820 return 1;
6821 }
6822
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006823 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006824
Avi Kivity6aa8b732006-12-10 02:21:36 -08006825 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006826 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6827 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006828 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006829}
6830
Avi Kivity851ba692009-08-24 11:10:17 +03006831static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006832{
Will Auld8fe8ab42012-11-29 12:42:12 -08006833 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006834 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6835 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6836 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006837
Will Auld8fe8ab42012-11-29 12:42:12 -08006838 msr.data = data;
6839 msr.index = ecx;
6840 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006841 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006842 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006843 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006844 return 1;
6845 }
6846
Avi Kivity59200272010-01-25 19:47:02 +02006847 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006848 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006849}
6850
Avi Kivity851ba692009-08-24 11:10:17 +03006851static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006852{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006853 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006854 return 1;
6855}
6856
Avi Kivity851ba692009-08-24 11:10:17 +03006857static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006858{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006859 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6860 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006861
Avi Kivity3842d132010-07-27 12:30:24 +03006862 kvm_make_request(KVM_REQ_EVENT, vcpu);
6863
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006864 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006865 return 1;
6866}
6867
Avi Kivity851ba692009-08-24 11:10:17 +03006868static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006869{
Avi Kivityd3bef152007-06-05 15:53:05 +03006870 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006871}
6872
Avi Kivity851ba692009-08-24 11:10:17 +03006873static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006874{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006875 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006876}
6877
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006878static int handle_invd(struct kvm_vcpu *vcpu)
6879{
Andre Przywara51d8b662010-12-21 11:12:02 +01006880 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006881}
6882
Avi Kivity851ba692009-08-24 11:10:17 +03006883static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006884{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006885 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006886
6887 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006888 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006889}
6890
Avi Kivityfee84b02011-11-10 14:57:25 +02006891static int handle_rdpmc(struct kvm_vcpu *vcpu)
6892{
6893 int err;
6894
6895 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006896 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006897}
6898
Avi Kivity851ba692009-08-24 11:10:17 +03006899static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006900{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006901 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006902}
6903
Dexuan Cui2acf9232010-06-10 11:27:12 +08006904static int handle_xsetbv(struct kvm_vcpu *vcpu)
6905{
6906 u64 new_bv = kvm_read_edx_eax(vcpu);
6907 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6908
6909 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006910 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006911 return 1;
6912}
6913
Wanpeng Lif53cd632014-12-02 19:14:58 +08006914static int handle_xsaves(struct kvm_vcpu *vcpu)
6915{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006916 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006917 WARN(1, "this should never happen\n");
6918 return 1;
6919}
6920
6921static int handle_xrstors(struct kvm_vcpu *vcpu)
6922{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006923 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006924 WARN(1, "this should never happen\n");
6925 return 1;
6926}
6927
Avi Kivity851ba692009-08-24 11:10:17 +03006928static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006929{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006930 if (likely(fasteoi)) {
6931 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6932 int access_type, offset;
6933
6934 access_type = exit_qualification & APIC_ACCESS_TYPE;
6935 offset = exit_qualification & APIC_ACCESS_OFFSET;
6936 /*
6937 * Sane guest uses MOV to write EOI, with written value
6938 * not cared. So make a short-circuit here by avoiding
6939 * heavy instruction emulation.
6940 */
6941 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6942 (offset == APIC_EOI)) {
6943 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006944 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006945 }
6946 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006947 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006948}
6949
Yang Zhangc7c9c562013-01-25 10:18:51 +08006950static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6951{
6952 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6953 int vector = exit_qualification & 0xff;
6954
6955 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6956 kvm_apic_set_eoi_accelerated(vcpu, vector);
6957 return 1;
6958}
6959
Yang Zhang83d4c282013-01-25 10:18:49 +08006960static int handle_apic_write(struct kvm_vcpu *vcpu)
6961{
6962 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6963 u32 offset = exit_qualification & 0xfff;
6964
6965 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6966 kvm_apic_write_nodecode(vcpu, offset);
6967 return 1;
6968}
6969
Avi Kivity851ba692009-08-24 11:10:17 +03006970static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006971{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006972 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006973 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006974 bool has_error_code = false;
6975 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006976 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006977 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006978
6979 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006980 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006981 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006982
6983 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6984
6985 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006986 if (reason == TASK_SWITCH_GATE && idt_v) {
6987 switch (type) {
6988 case INTR_TYPE_NMI_INTR:
6989 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006990 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006991 break;
6992 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006993 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006994 kvm_clear_interrupt_queue(vcpu);
6995 break;
6996 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006997 if (vmx->idt_vectoring_info &
6998 VECTORING_INFO_DELIVER_CODE_MASK) {
6999 has_error_code = true;
7000 error_code =
7001 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7002 }
7003 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007004 case INTR_TYPE_SOFT_EXCEPTION:
7005 kvm_clear_exception_queue(vcpu);
7006 break;
7007 default:
7008 break;
7009 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007010 }
Izik Eidus37817f22008-03-24 23:14:53 +02007011 tss_selector = exit_qualification;
7012
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007013 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7014 type != INTR_TYPE_EXT_INTR &&
7015 type != INTR_TYPE_NMI_INTR))
7016 skip_emulated_instruction(vcpu);
7017
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007018 if (kvm_task_switch(vcpu, tss_selector,
7019 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7020 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007021 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7022 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7023 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007024 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007025 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007026
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007027 /*
7028 * TODO: What about debug traps on tss switch?
7029 * Are we supposed to inject them and update dr6?
7030 */
7031
7032 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007033}
7034
Avi Kivity851ba692009-08-24 11:10:17 +03007035static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007036{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007037 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007038 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007039 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007040
Sheng Yangf9c617f2009-03-25 10:08:52 +08007041 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007042
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007043 /*
7044 * EPT violation happened while executing iret from NMI,
7045 * "blocked by NMI" bit has to be set before next VM entry.
7046 * There are errata that may cause this bit to not be set:
7047 * AAK134, BY25.
7048 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007049 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007050 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007051 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007052 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7053
Sheng Yang14394422008-04-28 12:24:45 +08007054 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007055 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007056
Junaid Shahid27959a42016-12-06 16:46:10 -08007057 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007058 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007059 ? PFERR_USER_MASK : 0;
7060 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007061 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007062 ? PFERR_WRITE_MASK : 0;
7063 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007064 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007065 ? PFERR_FETCH_MASK : 0;
7066 /* ept page table entry is present? */
7067 error_code |= (exit_qualification &
7068 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7069 EPT_VIOLATION_EXECUTABLE))
7070 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007071
Paolo Bonzinieebed242016-11-28 14:39:58 +01007072 error_code |= (exit_qualification & 0x100) != 0 ?
7073 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007074
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007075 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007076 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007077}
7078
Avi Kivity851ba692009-08-24 11:10:17 +03007079static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007080{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007081 gpa_t gpa;
7082
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007083 /*
7084 * A nested guest cannot optimize MMIO vmexits, because we have an
7085 * nGPA here instead of the required GPA.
7086 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007087 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007088 if (!is_guest_mode(vcpu) &&
7089 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007090 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007091 /*
7092 * Doing kvm_skip_emulated_instruction() depends on undefined
7093 * behavior: Intel's manual doesn't mandate
7094 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7095 * occurs and while on real hardware it was observed to be set,
7096 * other hypervisors (namely Hyper-V) don't set it, we end up
7097 * advancing IP with some random value. Disable fast mmio when
7098 * running nested and keep it for real hardware in hope that
7099 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7100 */
7101 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7102 return kvm_skip_emulated_instruction(vcpu);
7103 else
7104 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7105 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007106 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007107
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007108 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007109}
7110
Avi Kivity851ba692009-08-24 11:10:17 +03007111static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007112{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007113 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007114 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7115 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007116 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007117 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007118
7119 return 1;
7120}
7121
Mohammed Gamal80ced182009-09-01 12:48:18 +02007122static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007123{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007124 struct vcpu_vmx *vmx = to_vmx(vcpu);
7125 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007126 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007127 u32 cpu_exec_ctrl;
7128 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007129 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007130
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007131 /*
7132 * We should never reach the point where we are emulating L2
7133 * due to invalid guest state as that means we incorrectly
7134 * allowed a nested VMEntry with an invalid vmcs12.
7135 */
7136 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7137
Avi Kivity49e9d552010-09-19 14:34:08 +02007138 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7139 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007140
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007141 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007142 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007143 return handle_interrupt_window(&vmx->vcpu);
7144
Radim Krčmář72875d82017-04-26 22:32:19 +02007145 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007146 return 1;
7147
Liran Alon9b8ae632017-11-05 16:56:34 +02007148 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007149
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007150 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007151 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007152 ret = 0;
7153 goto out;
7154 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007155
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007156 if (err != EMULATE_DONE)
7157 goto emulation_error;
7158
7159 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7160 vcpu->arch.exception.pending)
7161 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007162
Gleb Natapov8d76c492013-05-08 18:38:44 +03007163 if (vcpu->arch.halt_request) {
7164 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007165 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007166 goto out;
7167 }
7168
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007169 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007170 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007171 if (need_resched())
7172 schedule();
7173 }
7174
Mohammed Gamal80ced182009-09-01 12:48:18 +02007175out:
7176 return ret;
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007177
7178emulation_error:
7179 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7180 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7181 vcpu->run->internal.ndata = 0;
7182 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007183}
7184
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007185static void grow_ple_window(struct kvm_vcpu *vcpu)
7186{
7187 struct vcpu_vmx *vmx = to_vmx(vcpu);
7188 int old = vmx->ple_window;
7189
Babu Mogerc8e88712018-03-16 16:37:24 -04007190 vmx->ple_window = __grow_ple_window(old, ple_window,
7191 ple_window_grow,
7192 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007193
7194 if (vmx->ple_window != old)
7195 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007196
7197 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007198}
7199
7200static void shrink_ple_window(struct kvm_vcpu *vcpu)
7201{
7202 struct vcpu_vmx *vmx = to_vmx(vcpu);
7203 int old = vmx->ple_window;
7204
Babu Mogerc8e88712018-03-16 16:37:24 -04007205 vmx->ple_window = __shrink_ple_window(old, ple_window,
7206 ple_window_shrink,
7207 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007208
7209 if (vmx->ple_window != old)
7210 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007211
7212 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007213}
7214
7215/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007216 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7217 */
7218static void wakeup_handler(void)
7219{
7220 struct kvm_vcpu *vcpu;
7221 int cpu = smp_processor_id();
7222
7223 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7224 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7225 blocked_vcpu_list) {
7226 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7227
7228 if (pi_test_on(pi_desc) == 1)
7229 kvm_vcpu_kick(vcpu);
7230 }
7231 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7232}
7233
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007234void vmx_enable_tdp(void)
7235{
7236 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7237 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7238 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7239 0ull, VMX_EPT_EXECUTABLE_MASK,
7240 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007241 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007242
7243 ept_set_mmio_spte_mask();
7244 kvm_enable_tdp();
7245}
7246
Tiejun Chenf2c76482014-10-28 10:14:47 +08007247static __init int hardware_setup(void)
7248{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007249 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007250
7251 rdmsrl_safe(MSR_EFER, &host_efer);
7252
7253 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7254 kvm_define_shared_msr(i, vmx_msr_index[i]);
7255
Radim Krčmář23611332016-09-29 22:41:33 +02007256 for (i = 0; i < VMX_BITMAP_NR; i++) {
7257 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7258 if (!vmx_bitmap[i])
7259 goto out;
7260 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007261
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007262 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7263 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7264
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007265 if (setup_vmcs_config(&vmcs_config) < 0) {
7266 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007267 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007268 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007269
7270 if (boot_cpu_has(X86_FEATURE_NX))
7271 kvm_enable_efer_bits(EFER_NX);
7272
Wanpeng Li08d839c2017-03-23 05:30:08 -07007273 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7274 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007275 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007276
Tiejun Chenf2c76482014-10-28 10:14:47 +08007277 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007278 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007279 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007280 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007281 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007282
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007283 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007284 enable_ept_ad_bits = 0;
7285
Wanpeng Li8ad81822017-10-09 15:51:53 -07007286 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007287 enable_unrestricted_guest = 0;
7288
Paolo Bonziniad15a292015-01-30 16:18:49 +01007289 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007290 flexpriority_enabled = 0;
7291
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007292 if (!cpu_has_virtual_nmis())
7293 enable_vnmi = 0;
7294
Paolo Bonziniad15a292015-01-30 16:18:49 +01007295 /*
7296 * set_apic_access_page_addr() is used to reload apic access
7297 * page upon invalidation. No need to do anything if not
7298 * using the APIC_ACCESS_ADDR VMCS field.
7299 */
7300 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007301 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007302
7303 if (!cpu_has_vmx_tpr_shadow())
7304 kvm_x86_ops->update_cr8_intercept = NULL;
7305
7306 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7307 kvm_disable_largepages();
7308
Wanpeng Li0f107682017-09-28 18:06:24 -07007309 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007310 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007311 ple_window = 0;
7312 ple_window_grow = 0;
7313 ple_window_max = 0;
7314 ple_window_shrink = 0;
7315 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007316
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007317 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007318 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007319 kvm_x86_ops->sync_pir_to_irr = NULL;
7320 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007321
Haozhong Zhang64903d62015-10-20 15:39:09 +08007322 if (cpu_has_vmx_tsc_scaling()) {
7323 kvm_has_tsc_control = true;
7324 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7325 kvm_tsc_scaling_ratio_frac_bits = 48;
7326 }
7327
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007328 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7329
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007330 if (enable_ept)
7331 vmx_enable_tdp();
7332 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007333 kvm_disable_tdp();
7334
Kai Huang843e4332015-01-28 10:54:28 +08007335 /*
7336 * Only enable PML when hardware supports PML feature, and both EPT
7337 * and EPT A/D bit features are enabled -- PML depends on them to work.
7338 */
7339 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7340 enable_pml = 0;
7341
7342 if (!enable_pml) {
7343 kvm_x86_ops->slot_enable_log_dirty = NULL;
7344 kvm_x86_ops->slot_disable_log_dirty = NULL;
7345 kvm_x86_ops->flush_log_dirty = NULL;
7346 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7347 }
7348
Yunhong Jiang64672c92016-06-13 14:19:59 -07007349 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7350 u64 vmx_msr;
7351
7352 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7353 cpu_preemption_timer_multi =
7354 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7355 } else {
7356 kvm_x86_ops->set_hv_timer = NULL;
7357 kvm_x86_ops->cancel_hv_timer = NULL;
7358 }
7359
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007360 if (!cpu_has_vmx_shadow_vmcs())
7361 enable_shadow_vmcs = 0;
7362 if (enable_shadow_vmcs)
7363 init_vmcs_shadow_fields();
7364
Feng Wubf9f6ac2015-09-18 22:29:55 +08007365 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007366 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007367
Ashok Rajc45dcc72016-06-22 14:59:56 +08007368 kvm_mce_cap_supported |= MCG_LMCE_P;
7369
Tiejun Chenf2c76482014-10-28 10:14:47 +08007370 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007371
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007372out:
Radim Krčmář23611332016-09-29 22:41:33 +02007373 for (i = 0; i < VMX_BITMAP_NR; i++)
7374 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007375
7376 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007377}
7378
7379static __exit void hardware_unsetup(void)
7380{
Radim Krčmář23611332016-09-29 22:41:33 +02007381 int i;
7382
7383 for (i = 0; i < VMX_BITMAP_NR; i++)
7384 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007385
Tiejun Chenf2c76482014-10-28 10:14:47 +08007386 free_kvm_area();
7387}
7388
Avi Kivity6aa8b732006-12-10 02:21:36 -08007389/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007390 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7391 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7392 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007393static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007394{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007395 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007396 grow_ple_window(vcpu);
7397
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007398 /*
7399 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7400 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7401 * never set PAUSE_EXITING and just set PLE if supported,
7402 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7403 */
7404 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007405 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007406}
7407
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007408static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007409{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007410 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007411}
7412
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007413static int handle_mwait(struct kvm_vcpu *vcpu)
7414{
7415 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7416 return handle_nop(vcpu);
7417}
7418
Jim Mattson45ec3682017-08-23 16:32:04 -07007419static int handle_invalid_op(struct kvm_vcpu *vcpu)
7420{
7421 kvm_queue_exception(vcpu, UD_VECTOR);
7422 return 1;
7423}
7424
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007425static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7426{
7427 return 1;
7428}
7429
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007430static int handle_monitor(struct kvm_vcpu *vcpu)
7431{
7432 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7433 return handle_nop(vcpu);
7434}
7435
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007436/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007437 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7438 * set the success or error code of an emulated VMX instruction, as specified
7439 * by Vol 2B, VMX Instruction Reference, "Conventions".
7440 */
7441static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7442{
7443 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7444 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7445 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7446}
7447
7448static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7449{
7450 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7451 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7452 X86_EFLAGS_SF | X86_EFLAGS_OF))
7453 | X86_EFLAGS_CF);
7454}
7455
Abel Gordon145c28d2013-04-18 14:36:55 +03007456static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007457 u32 vm_instruction_error)
7458{
7459 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7460 /*
7461 * failValid writes the error number to the current VMCS, which
7462 * can't be done there isn't a current VMCS.
7463 */
7464 nested_vmx_failInvalid(vcpu);
7465 return;
7466 }
7467 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7468 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7469 X86_EFLAGS_SF | X86_EFLAGS_OF))
7470 | X86_EFLAGS_ZF);
7471 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7472 /*
7473 * We don't need to force a shadow sync because
7474 * VM_INSTRUCTION_ERROR is not shadowed
7475 */
7476}
Abel Gordon145c28d2013-04-18 14:36:55 +03007477
Wincy Vanff651cb2014-12-11 08:52:58 +03007478static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7479{
7480 /* TODO: not to reset guest simply here. */
7481 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007482 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007483}
7484
Jan Kiszkaf41245002014-03-07 20:03:13 +01007485static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7486{
7487 struct vcpu_vmx *vmx =
7488 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7489
7490 vmx->nested.preemption_timer_expired = true;
7491 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7492 kvm_vcpu_kick(&vmx->vcpu);
7493
7494 return HRTIMER_NORESTART;
7495}
7496
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007497/*
Bandan Das19677e32014-05-06 02:19:15 -04007498 * Decode the memory-address operand of a vmx instruction, as recorded on an
7499 * exit caused by such an instruction (run by a guest hypervisor).
7500 * On success, returns 0. When the operand is invalid, returns 1 and throws
7501 * #UD or #GP.
7502 */
7503static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7504 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007505 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007506{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007507 gva_t off;
7508 bool exn;
7509 struct kvm_segment s;
7510
Bandan Das19677e32014-05-06 02:19:15 -04007511 /*
7512 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7513 * Execution", on an exit, vmx_instruction_info holds most of the
7514 * addressing components of the operand. Only the displacement part
7515 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7516 * For how an actual address is calculated from all these components,
7517 * refer to Vol. 1, "Operand Addressing".
7518 */
7519 int scaling = vmx_instruction_info & 3;
7520 int addr_size = (vmx_instruction_info >> 7) & 7;
7521 bool is_reg = vmx_instruction_info & (1u << 10);
7522 int seg_reg = (vmx_instruction_info >> 15) & 7;
7523 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7524 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7525 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7526 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7527
7528 if (is_reg) {
7529 kvm_queue_exception(vcpu, UD_VECTOR);
7530 return 1;
7531 }
7532
7533 /* Addr = segment_base + offset */
7534 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007535 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007536 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007537 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007538 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007539 off += kvm_register_read(vcpu, index_reg)<<scaling;
7540 vmx_get_segment(vcpu, &s, seg_reg);
7541 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007542
7543 if (addr_size == 1) /* 32 bit */
7544 *ret &= 0xffffffff;
7545
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007546 /* Checks for #GP/#SS exceptions. */
7547 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007548 if (is_long_mode(vcpu)) {
7549 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7550 * non-canonical form. This is the only check on the memory
7551 * destination for long mode!
7552 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007553 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007554 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007555 /* Protected mode: apply checks for segment validity in the
7556 * following order:
7557 * - segment type check (#GP(0) may be thrown)
7558 * - usability check (#GP(0)/#SS(0))
7559 * - limit check (#GP(0)/#SS(0))
7560 */
7561 if (wr)
7562 /* #GP(0) if the destination operand is located in a
7563 * read-only data segment or any code segment.
7564 */
7565 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7566 else
7567 /* #GP(0) if the source operand is located in an
7568 * execute-only code segment
7569 */
7570 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007571 if (exn) {
7572 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7573 return 1;
7574 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007575 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7576 */
7577 exn = (s.unusable != 0);
7578 /* Protected mode: #GP(0)/#SS(0) if the memory
7579 * operand is outside the segment limit.
7580 */
7581 exn = exn || (off + sizeof(u64) > s.limit);
7582 }
7583 if (exn) {
7584 kvm_queue_exception_e(vcpu,
7585 seg_reg == VCPU_SREG_SS ?
7586 SS_VECTOR : GP_VECTOR,
7587 0);
7588 return 1;
7589 }
7590
Bandan Das19677e32014-05-06 02:19:15 -04007591 return 0;
7592}
7593
Radim Krčmářcbf71272017-05-19 15:48:51 +02007594static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007595{
7596 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007597 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007598
7599 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007600 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007601 return 1;
7602
Radim Krčmářcbf71272017-05-19 15:48:51 +02007603 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7604 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007605 kvm_inject_page_fault(vcpu, &e);
7606 return 1;
7607 }
7608
Bandan Das3573e222014-05-06 02:19:16 -04007609 return 0;
7610}
7611
Jim Mattsone29acc52016-11-30 12:03:43 -08007612static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7613{
7614 struct vcpu_vmx *vmx = to_vmx(vcpu);
7615 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007616 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007617
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007618 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7619 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007620 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007621
7622 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7623 if (!vmx->nested.cached_vmcs12)
7624 goto out_cached_vmcs12;
7625
7626 if (enable_shadow_vmcs) {
7627 shadow_vmcs = alloc_vmcs();
7628 if (!shadow_vmcs)
7629 goto out_shadow_vmcs;
7630 /* mark vmcs as shadow */
7631 shadow_vmcs->revision_id |= (1u << 31);
7632 /* init shadow vmcs */
7633 vmcs_clear(shadow_vmcs);
7634 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7635 }
7636
Jim Mattsone29acc52016-11-30 12:03:43 -08007637 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7638 HRTIMER_MODE_REL_PINNED);
7639 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7640
7641 vmx->nested.vmxon = true;
7642 return 0;
7643
7644out_shadow_vmcs:
7645 kfree(vmx->nested.cached_vmcs12);
7646
7647out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007648 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007649
Jim Mattsonde3a0022017-11-27 17:22:25 -06007650out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007651 return -ENOMEM;
7652}
7653
Bandan Das3573e222014-05-06 02:19:16 -04007654/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007655 * Emulate the VMXON instruction.
7656 * Currently, we just remember that VMX is active, and do not save or even
7657 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7658 * do not currently need to store anything in that guest-allocated memory
7659 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7660 * argument is different from the VMXON pointer (which the spec says they do).
7661 */
7662static int handle_vmon(struct kvm_vcpu *vcpu)
7663{
Jim Mattsone29acc52016-11-30 12:03:43 -08007664 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007665 gpa_t vmptr;
7666 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007667 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007668 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7669 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007670
Jim Mattson70f3aac2017-04-26 08:53:46 -07007671 /*
7672 * The Intel VMX Instruction Reference lists a bunch of bits that are
7673 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7674 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7675 * Otherwise, we should fail with #UD. But most faulting conditions
7676 * have already been checked by hardware, prior to the VM-exit for
7677 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7678 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007679 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007680 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007681 kvm_queue_exception(vcpu, UD_VECTOR);
7682 return 1;
7683 }
7684
Abel Gordon145c28d2013-04-18 14:36:55 +03007685 if (vmx->nested.vmxon) {
7686 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007687 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007688 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007689
Haozhong Zhang3b840802016-06-22 14:59:54 +08007690 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007691 != VMXON_NEEDED_FEATURES) {
7692 kvm_inject_gp(vcpu, 0);
7693 return 1;
7694 }
7695
Radim Krčmářcbf71272017-05-19 15:48:51 +02007696 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007697 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007698
7699 /*
7700 * SDM 3: 24.11.5
7701 * The first 4 bytes of VMXON region contain the supported
7702 * VMCS revision identifier
7703 *
7704 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7705 * which replaces physical address width with 32
7706 */
7707 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7708 nested_vmx_failInvalid(vcpu);
7709 return kvm_skip_emulated_instruction(vcpu);
7710 }
7711
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007712 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7713 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007714 nested_vmx_failInvalid(vcpu);
7715 return kvm_skip_emulated_instruction(vcpu);
7716 }
7717 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7718 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007719 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007720 nested_vmx_failInvalid(vcpu);
7721 return kvm_skip_emulated_instruction(vcpu);
7722 }
7723 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007724 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007725
7726 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007727 ret = enter_vmx_operation(vcpu);
7728 if (ret)
7729 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007730
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007731 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007732 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007733}
7734
7735/*
7736 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7737 * for running VMX instructions (except VMXON, whose prerequisites are
7738 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007739 * Note that many of these exceptions have priority over VM exits, so they
7740 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007741 */
7742static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7743{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007744 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007745 kvm_queue_exception(vcpu, UD_VECTOR);
7746 return 0;
7747 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007748 return 1;
7749}
7750
David Matlack8ca44e82017-08-01 14:00:39 -07007751static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7752{
7753 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7754 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7755}
7756
Abel Gordone7953d72013-04-18 14:37:55 +03007757static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7758{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007759 if (vmx->nested.current_vmptr == -1ull)
7760 return;
7761
Abel Gordon012f83c2013-04-18 14:39:25 +03007762 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007763 /* copy to memory all shadowed fields in case
7764 they were modified */
7765 copy_shadow_to_vmcs12(vmx);
7766 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007767 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007768 }
Wincy Van705699a2015-02-03 23:58:17 +08007769 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007770
7771 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007772 kvm_vcpu_write_guest_page(&vmx->vcpu,
7773 vmx->nested.current_vmptr >> PAGE_SHIFT,
7774 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007775
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007776 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007777}
7778
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007779/*
7780 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7781 * just stops using VMX.
7782 */
7783static void free_nested(struct vcpu_vmx *vmx)
7784{
Wanpeng Lib7455822017-11-22 14:04:00 -08007785 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007786 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007787
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007788 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007789 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007790 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007791 vmx->nested.posted_intr_nv = -1;
7792 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007793 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007794 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007795 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7796 free_vmcs(vmx->vmcs01.shadow_vmcs);
7797 vmx->vmcs01.shadow_vmcs = NULL;
7798 }
David Matlack4f2777b2016-07-13 17:16:37 -07007799 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007800 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007801 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007802 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007803 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007804 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007805 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007806 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007807 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007808 }
Wincy Van705699a2015-02-03 23:58:17 +08007809 if (vmx->nested.pi_desc_page) {
7810 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007811 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007812 vmx->nested.pi_desc_page = NULL;
7813 vmx->nested.pi_desc = NULL;
7814 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007815
Jim Mattsonde3a0022017-11-27 17:22:25 -06007816 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007817}
7818
7819/* Emulate the VMXOFF instruction */
7820static int handle_vmoff(struct kvm_vcpu *vcpu)
7821{
7822 if (!nested_vmx_check_permission(vcpu))
7823 return 1;
7824 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007825 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007826 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007827}
7828
Nadav Har'El27d6c862011-05-25 23:06:59 +03007829/* Emulate the VMCLEAR instruction */
7830static int handle_vmclear(struct kvm_vcpu *vcpu)
7831{
7832 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007833 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007834 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007835
7836 if (!nested_vmx_check_permission(vcpu))
7837 return 1;
7838
Radim Krčmářcbf71272017-05-19 15:48:51 +02007839 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007840 return 1;
7841
Radim Krčmářcbf71272017-05-19 15:48:51 +02007842 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7843 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7844 return kvm_skip_emulated_instruction(vcpu);
7845 }
7846
7847 if (vmptr == vmx->nested.vmxon_ptr) {
7848 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7849 return kvm_skip_emulated_instruction(vcpu);
7850 }
7851
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007852 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007853 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007854
Jim Mattson587d7e722017-03-02 12:41:48 -08007855 kvm_vcpu_write_guest(vcpu,
7856 vmptr + offsetof(struct vmcs12, launch_state),
7857 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007858
Nadav Har'El27d6c862011-05-25 23:06:59 +03007859 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007860 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007861}
7862
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007863static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7864
7865/* Emulate the VMLAUNCH instruction */
7866static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7867{
7868 return nested_vmx_run(vcpu, true);
7869}
7870
7871/* Emulate the VMRESUME instruction */
7872static int handle_vmresume(struct kvm_vcpu *vcpu)
7873{
7874
7875 return nested_vmx_run(vcpu, false);
7876}
7877
Nadav Har'El49f705c2011-05-25 23:08:30 +03007878/*
7879 * Read a vmcs12 field. Since these can have varying lengths and we return
7880 * one type, we chose the biggest type (u64) and zero-extend the return value
7881 * to that size. Note that the caller, handle_vmread, might need to use only
7882 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7883 * 64-bit fields are to be returned).
7884 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007885static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7886 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007887{
7888 short offset = vmcs_field_to_offset(field);
7889 char *p;
7890
7891 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007892 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007893
7894 p = ((char *)(get_vmcs12(vcpu))) + offset;
7895
Jim Mattsond37f4262017-12-22 12:12:16 -08007896 switch (vmcs_field_width(field)) {
7897 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007898 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007899 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007900 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007901 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007902 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007903 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007904 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007905 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007906 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007907 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007908 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007909 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007910 WARN_ON(1);
7911 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007912 }
7913}
7914
Abel Gordon20b97fe2013-04-18 14:36:25 +03007915
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007916static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7917 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007918 short offset = vmcs_field_to_offset(field);
7919 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7920 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007921 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007922
Jim Mattsond37f4262017-12-22 12:12:16 -08007923 switch (vmcs_field_width(field)) {
7924 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007925 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007926 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007927 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007928 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007929 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007930 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007931 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007932 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007933 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007934 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007935 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007936 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007937 WARN_ON(1);
7938 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007939 }
7940
7941}
7942
Abel Gordon16f5b902013-04-18 14:38:25 +03007943static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7944{
7945 int i;
7946 unsigned long field;
7947 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007948 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007949 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007950 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007951
Jan Kiszka282da872014-10-08 18:05:39 +02007952 preempt_disable();
7953
Abel Gordon16f5b902013-04-18 14:38:25 +03007954 vmcs_load(shadow_vmcs);
7955
7956 for (i = 0; i < num_fields; i++) {
7957 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007958 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007959 vmcs12_write_any(&vmx->vcpu, field, field_value);
7960 }
7961
7962 vmcs_clear(shadow_vmcs);
7963 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007964
7965 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007966}
7967
Abel Gordonc3114422013-04-18 14:38:55 +03007968static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7969{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007970 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007971 shadow_read_write_fields,
7972 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007973 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007974 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007975 max_shadow_read_write_fields,
7976 max_shadow_read_only_fields
7977 };
7978 int i, q;
7979 unsigned long field;
7980 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007981 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007982
7983 vmcs_load(shadow_vmcs);
7984
Mathias Krausec2bae892013-06-26 20:36:21 +02007985 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007986 for (i = 0; i < max_fields[q]; i++) {
7987 field = fields[q][i];
7988 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007989 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007990 }
7991 }
7992
7993 vmcs_clear(shadow_vmcs);
7994 vmcs_load(vmx->loaded_vmcs->vmcs);
7995}
7996
Nadav Har'El49f705c2011-05-25 23:08:30 +03007997/*
7998 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7999 * used before) all generate the same failure when it is missing.
8000 */
8001static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8002{
8003 struct vcpu_vmx *vmx = to_vmx(vcpu);
8004 if (vmx->nested.current_vmptr == -1ull) {
8005 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008006 return 0;
8007 }
8008 return 1;
8009}
8010
8011static int handle_vmread(struct kvm_vcpu *vcpu)
8012{
8013 unsigned long field;
8014 u64 field_value;
8015 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8016 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8017 gva_t gva = 0;
8018
Kyle Hueyeb277562016-11-29 12:40:39 -08008019 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008020 return 1;
8021
Kyle Huey6affcbe2016-11-29 12:40:40 -08008022 if (!nested_vmx_check_vmcs12(vcpu))
8023 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008024
Nadav Har'El49f705c2011-05-25 23:08:30 +03008025 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008026 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008027 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008028 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008029 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008030 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008031 }
8032 /*
8033 * Now copy part of this value to register or memory, as requested.
8034 * Note that the number of bits actually copied is 32 or 64 depending
8035 * on the guest's mode (32 or 64 bit), not on the given field's length.
8036 */
8037 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008038 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008039 field_value);
8040 } else {
8041 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008042 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008043 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008044 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03008045 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
8046 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
8047 }
8048
8049 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008050 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008051}
8052
8053
8054static int handle_vmwrite(struct kvm_vcpu *vcpu)
8055{
8056 unsigned long field;
8057 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008058 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008059 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8060 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008061
Nadav Har'El49f705c2011-05-25 23:08:30 +03008062 /* The value to write might be 32 or 64 bits, depending on L1's long
8063 * mode, and eventually we need to write that into a field of several
8064 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008065 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008066 * bits into the vmcs12 field.
8067 */
8068 u64 field_value = 0;
8069 struct x86_exception e;
8070
Kyle Hueyeb277562016-11-29 12:40:39 -08008071 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008072 return 1;
8073
Kyle Huey6affcbe2016-11-29 12:40:40 -08008074 if (!nested_vmx_check_vmcs12(vcpu))
8075 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008076
Nadav Har'El49f705c2011-05-25 23:08:30 +03008077 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008078 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008079 (((vmx_instruction_info) >> 3) & 0xf));
8080 else {
8081 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008082 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008083 return 1;
8084 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03008085 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008086 kvm_inject_page_fault(vcpu, &e);
8087 return 1;
8088 }
8089 }
8090
8091
Nadav Amit27e6fb52014-06-18 17:19:26 +03008092 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008093 if (vmcs_field_readonly(field)) {
8094 nested_vmx_failValid(vcpu,
8095 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008096 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008097 }
8098
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008099 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008100 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008101 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008102 }
8103
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008104 switch (field) {
8105#define SHADOW_FIELD_RW(x) case x:
8106#include "vmx_shadow_fields.h"
8107 /*
8108 * The fields that can be updated by L1 without a vmexit are
8109 * always updated in the vmcs02, the others go down the slow
8110 * path of prepare_vmcs02.
8111 */
8112 break;
8113 default:
8114 vmx->nested.dirty_vmcs12 = true;
8115 break;
8116 }
8117
Nadav Har'El49f705c2011-05-25 23:08:30 +03008118 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008119 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008120}
8121
Jim Mattsona8bc2842016-11-30 12:03:44 -08008122static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8123{
8124 vmx->nested.current_vmptr = vmptr;
8125 if (enable_shadow_vmcs) {
8126 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8127 SECONDARY_EXEC_SHADOW_VMCS);
8128 vmcs_write64(VMCS_LINK_POINTER,
8129 __pa(vmx->vmcs01.shadow_vmcs));
8130 vmx->nested.sync_shadow_vmcs = true;
8131 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008132 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008133}
8134
Nadav Har'El63846662011-05-25 23:07:29 +03008135/* Emulate the VMPTRLD instruction */
8136static int handle_vmptrld(struct kvm_vcpu *vcpu)
8137{
8138 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008139 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008140
8141 if (!nested_vmx_check_permission(vcpu))
8142 return 1;
8143
Radim Krčmářcbf71272017-05-19 15:48:51 +02008144 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008145 return 1;
8146
Radim Krčmářcbf71272017-05-19 15:48:51 +02008147 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8148 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8149 return kvm_skip_emulated_instruction(vcpu);
8150 }
8151
8152 if (vmptr == vmx->nested.vmxon_ptr) {
8153 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8154 return kvm_skip_emulated_instruction(vcpu);
8155 }
8156
Nadav Har'El63846662011-05-25 23:07:29 +03008157 if (vmx->nested.current_vmptr != vmptr) {
8158 struct vmcs12 *new_vmcs12;
8159 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008160 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8161 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008162 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008163 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008164 }
8165 new_vmcs12 = kmap(page);
8166 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8167 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008168 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008169 nested_vmx_failValid(vcpu,
8170 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008171 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008172 }
Nadav Har'El63846662011-05-25 23:07:29 +03008173
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008174 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008175 /*
8176 * Load VMCS12 from guest memory since it is not already
8177 * cached.
8178 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008179 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8180 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008181 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008182
Jim Mattsona8bc2842016-11-30 12:03:44 -08008183 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008184 }
8185
8186 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008187 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008188}
8189
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008190/* Emulate the VMPTRST instruction */
8191static int handle_vmptrst(struct kvm_vcpu *vcpu)
8192{
8193 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8194 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8195 gva_t vmcs_gva;
8196 struct x86_exception e;
8197
8198 if (!nested_vmx_check_permission(vcpu))
8199 return 1;
8200
8201 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008202 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008203 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008204 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008205 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8206 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8207 sizeof(u64), &e)) {
8208 kvm_inject_page_fault(vcpu, &e);
8209 return 1;
8210 }
8211 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008212 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008213}
8214
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008215/* Emulate the INVEPT instruction */
8216static int handle_invept(struct kvm_vcpu *vcpu)
8217{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008218 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008219 u32 vmx_instruction_info, types;
8220 unsigned long type;
8221 gva_t gva;
8222 struct x86_exception e;
8223 struct {
8224 u64 eptp, gpa;
8225 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008226
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008227 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008228 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008229 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008230 kvm_queue_exception(vcpu, UD_VECTOR);
8231 return 1;
8232 }
8233
8234 if (!nested_vmx_check_permission(vcpu))
8235 return 1;
8236
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008237 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008238 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008239
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008240 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008241
Jim Mattson85c856b2016-10-26 08:38:38 -07008242 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008243 nested_vmx_failValid(vcpu,
8244 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008245 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008246 }
8247
8248 /* According to the Intel VMX instruction reference, the memory
8249 * operand is read even if it isn't needed (e.g., for type==global)
8250 */
8251 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008252 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008253 return 1;
8254 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8255 sizeof(operand), &e)) {
8256 kvm_inject_page_fault(vcpu, &e);
8257 return 1;
8258 }
8259
8260 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008261 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008262 /*
8263 * TODO: track mappings and invalidate
8264 * single context requests appropriately
8265 */
8266 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008267 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008268 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008269 nested_vmx_succeed(vcpu);
8270 break;
8271 default:
8272 BUG_ON(1);
8273 break;
8274 }
8275
Kyle Huey6affcbe2016-11-29 12:40:40 -08008276 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008277}
8278
Petr Matouseka642fc32014-09-23 20:22:30 +02008279static int handle_invvpid(struct kvm_vcpu *vcpu)
8280{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008281 struct vcpu_vmx *vmx = to_vmx(vcpu);
8282 u32 vmx_instruction_info;
8283 unsigned long type, types;
8284 gva_t gva;
8285 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008286 struct {
8287 u64 vpid;
8288 u64 gla;
8289 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008290
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008291 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008292 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008293 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008294 kvm_queue_exception(vcpu, UD_VECTOR);
8295 return 1;
8296 }
8297
8298 if (!nested_vmx_check_permission(vcpu))
8299 return 1;
8300
8301 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8302 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8303
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008304 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008305 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008306
Jim Mattson85c856b2016-10-26 08:38:38 -07008307 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008308 nested_vmx_failValid(vcpu,
8309 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008310 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008311 }
8312
8313 /* according to the intel vmx instruction reference, the memory
8314 * operand is read even if it isn't needed (e.g., for type==global)
8315 */
8316 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8317 vmx_instruction_info, false, &gva))
8318 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008319 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8320 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008321 kvm_inject_page_fault(vcpu, &e);
8322 return 1;
8323 }
Jim Mattson40352602017-06-28 09:37:37 -07008324 if (operand.vpid >> 16) {
8325 nested_vmx_failValid(vcpu,
8326 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8327 return kvm_skip_emulated_instruction(vcpu);
8328 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008329
8330 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008331 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008332 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008333 nested_vmx_failValid(vcpu,
8334 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8335 return kvm_skip_emulated_instruction(vcpu);
8336 }
8337 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008338 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008339 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008340 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008341 nested_vmx_failValid(vcpu,
8342 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008343 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008344 }
8345 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008346 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008347 break;
8348 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008349 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008350 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008351 }
8352
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008353 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008354 nested_vmx_succeed(vcpu);
8355
Kyle Huey6affcbe2016-11-29 12:40:40 -08008356 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008357}
8358
Kai Huang843e4332015-01-28 10:54:28 +08008359static int handle_pml_full(struct kvm_vcpu *vcpu)
8360{
8361 unsigned long exit_qualification;
8362
8363 trace_kvm_pml_full(vcpu->vcpu_id);
8364
8365 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8366
8367 /*
8368 * PML buffer FULL happened while executing iret from NMI,
8369 * "blocked by NMI" bit has to be set before next VM entry.
8370 */
8371 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008372 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008373 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8374 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8375 GUEST_INTR_STATE_NMI);
8376
8377 /*
8378 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8379 * here.., and there's no userspace involvement needed for PML.
8380 */
8381 return 1;
8382}
8383
Yunhong Jiang64672c92016-06-13 14:19:59 -07008384static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8385{
8386 kvm_lapic_expired_hv_timer(vcpu);
8387 return 1;
8388}
8389
Bandan Das41ab9372017-08-03 15:54:43 -04008390static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8391{
8392 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008393 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8394
8395 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008396 switch (address & VMX_EPTP_MT_MASK) {
8397 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008398 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008399 return false;
8400 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008401 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008402 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008403 return false;
8404 break;
8405 default:
8406 return false;
8407 }
8408
David Hildenbrandbb97a012017-08-10 23:15:28 +02008409 /* only 4 levels page-walk length are valid */
8410 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008411 return false;
8412
8413 /* Reserved bits should not be set */
8414 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8415 return false;
8416
8417 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008418 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008419 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008420 return false;
8421 }
8422
8423 return true;
8424}
8425
8426static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8427 struct vmcs12 *vmcs12)
8428{
8429 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8430 u64 address;
8431 bool accessed_dirty;
8432 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8433
8434 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8435 !nested_cpu_has_ept(vmcs12))
8436 return 1;
8437
8438 if (index >= VMFUNC_EPTP_ENTRIES)
8439 return 1;
8440
8441
8442 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8443 &address, index * 8, 8))
8444 return 1;
8445
David Hildenbrandbb97a012017-08-10 23:15:28 +02008446 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008447
8448 /*
8449 * If the (L2) guest does a vmfunc to the currently
8450 * active ept pointer, we don't have to do anything else
8451 */
8452 if (vmcs12->ept_pointer != address) {
8453 if (!valid_ept_address(vcpu, address))
8454 return 1;
8455
8456 kvm_mmu_unload(vcpu);
8457 mmu->ept_ad = accessed_dirty;
8458 mmu->base_role.ad_disabled = !accessed_dirty;
8459 vmcs12->ept_pointer = address;
8460 /*
8461 * TODO: Check what's the correct approach in case
8462 * mmu reload fails. Currently, we just let the next
8463 * reload potentially fail
8464 */
8465 kvm_mmu_reload(vcpu);
8466 }
8467
8468 return 0;
8469}
8470
Bandan Das2a499e42017-08-03 15:54:41 -04008471static int handle_vmfunc(struct kvm_vcpu *vcpu)
8472{
Bandan Das27c42a12017-08-03 15:54:42 -04008473 struct vcpu_vmx *vmx = to_vmx(vcpu);
8474 struct vmcs12 *vmcs12;
8475 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8476
8477 /*
8478 * VMFUNC is only supported for nested guests, but we always enable the
8479 * secondary control for simplicity; for non-nested mode, fake that we
8480 * didn't by injecting #UD.
8481 */
8482 if (!is_guest_mode(vcpu)) {
8483 kvm_queue_exception(vcpu, UD_VECTOR);
8484 return 1;
8485 }
8486
8487 vmcs12 = get_vmcs12(vcpu);
8488 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8489 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008490
8491 switch (function) {
8492 case 0:
8493 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8494 goto fail;
8495 break;
8496 default:
8497 goto fail;
8498 }
8499 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008500
8501fail:
8502 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8503 vmcs_read32(VM_EXIT_INTR_INFO),
8504 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008505 return 1;
8506}
8507
Nadav Har'El0140cae2011-05-25 23:06:28 +03008508/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008509 * The exit handlers return 1 if the exit was handled fully and guest execution
8510 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8511 * to be done to userspace and return 0.
8512 */
Mathias Krause772e0312012-08-30 01:30:19 +02008513static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008514 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8515 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008516 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008517 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008518 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008519 [EXIT_REASON_CR_ACCESS] = handle_cr,
8520 [EXIT_REASON_DR_ACCESS] = handle_dr,
8521 [EXIT_REASON_CPUID] = handle_cpuid,
8522 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8523 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8524 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8525 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008526 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008527 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008528 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008529 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008530 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008531 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008532 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008533 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008534 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008535 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008536 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008537 [EXIT_REASON_VMOFF] = handle_vmoff,
8538 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008539 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8540 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008541 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008542 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008543 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008544 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008545 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008546 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008547 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8548 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008549 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8550 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008551 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008552 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008553 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008554 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008555 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008556 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008557 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008558 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008559 [EXIT_REASON_XSAVES] = handle_xsaves,
8560 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008561 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008562 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008563 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008564};
8565
8566static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008567 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008568
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008569static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8570 struct vmcs12 *vmcs12)
8571{
8572 unsigned long exit_qualification;
8573 gpa_t bitmap, last_bitmap;
8574 unsigned int port;
8575 int size;
8576 u8 b;
8577
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008578 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008579 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008580
8581 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8582
8583 port = exit_qualification >> 16;
8584 size = (exit_qualification & 7) + 1;
8585
8586 last_bitmap = (gpa_t)-1;
8587 b = -1;
8588
8589 while (size > 0) {
8590 if (port < 0x8000)
8591 bitmap = vmcs12->io_bitmap_a;
8592 else if (port < 0x10000)
8593 bitmap = vmcs12->io_bitmap_b;
8594 else
Joe Perches1d804d02015-03-30 16:46:09 -07008595 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008596 bitmap += (port & 0x7fff) / 8;
8597
8598 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008599 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008600 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008601 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008602 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008603
8604 port++;
8605 size--;
8606 last_bitmap = bitmap;
8607 }
8608
Joe Perches1d804d02015-03-30 16:46:09 -07008609 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008610}
8611
Nadav Har'El644d7112011-05-25 23:12:35 +03008612/*
8613 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8614 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8615 * disinterest in the current event (read or write a specific MSR) by using an
8616 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8617 */
8618static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8619 struct vmcs12 *vmcs12, u32 exit_reason)
8620{
8621 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8622 gpa_t bitmap;
8623
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008624 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008625 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008626
8627 /*
8628 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8629 * for the four combinations of read/write and low/high MSR numbers.
8630 * First we need to figure out which of the four to use:
8631 */
8632 bitmap = vmcs12->msr_bitmap;
8633 if (exit_reason == EXIT_REASON_MSR_WRITE)
8634 bitmap += 2048;
8635 if (msr_index >= 0xc0000000) {
8636 msr_index -= 0xc0000000;
8637 bitmap += 1024;
8638 }
8639
8640 /* Then read the msr_index'th bit from this bitmap: */
8641 if (msr_index < 1024*8) {
8642 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008643 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008644 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008645 return 1 & (b >> (msr_index & 7));
8646 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008647 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008648}
8649
8650/*
8651 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8652 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8653 * intercept (via guest_host_mask etc.) the current event.
8654 */
8655static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8656 struct vmcs12 *vmcs12)
8657{
8658 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8659 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008660 int reg;
8661 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008662
8663 switch ((exit_qualification >> 4) & 3) {
8664 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008665 reg = (exit_qualification >> 8) & 15;
8666 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008667 switch (cr) {
8668 case 0:
8669 if (vmcs12->cr0_guest_host_mask &
8670 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008671 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008672 break;
8673 case 3:
8674 if ((vmcs12->cr3_target_count >= 1 &&
8675 vmcs12->cr3_target_value0 == val) ||
8676 (vmcs12->cr3_target_count >= 2 &&
8677 vmcs12->cr3_target_value1 == val) ||
8678 (vmcs12->cr3_target_count >= 3 &&
8679 vmcs12->cr3_target_value2 == val) ||
8680 (vmcs12->cr3_target_count >= 4 &&
8681 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008682 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008683 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008684 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008685 break;
8686 case 4:
8687 if (vmcs12->cr4_guest_host_mask &
8688 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008689 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008690 break;
8691 case 8:
8692 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008693 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008694 break;
8695 }
8696 break;
8697 case 2: /* clts */
8698 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8699 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008700 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008701 break;
8702 case 1: /* mov from cr */
8703 switch (cr) {
8704 case 3:
8705 if (vmcs12->cpu_based_vm_exec_control &
8706 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008707 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008708 break;
8709 case 8:
8710 if (vmcs12->cpu_based_vm_exec_control &
8711 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008712 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008713 break;
8714 }
8715 break;
8716 case 3: /* lmsw */
8717 /*
8718 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8719 * cr0. Other attempted changes are ignored, with no exit.
8720 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008721 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008722 if (vmcs12->cr0_guest_host_mask & 0xe &
8723 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008724 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008725 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8726 !(vmcs12->cr0_read_shadow & 0x1) &&
8727 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008728 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008729 break;
8730 }
Joe Perches1d804d02015-03-30 16:46:09 -07008731 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008732}
8733
8734/*
8735 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8736 * should handle it ourselves in L0 (and then continue L2). Only call this
8737 * when in is_guest_mode (L2).
8738 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008739static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008740{
Nadav Har'El644d7112011-05-25 23:12:35 +03008741 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8742 struct vcpu_vmx *vmx = to_vmx(vcpu);
8743 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8744
Jim Mattson4f350c62017-09-14 16:31:44 -07008745 if (vmx->nested.nested_run_pending)
8746 return false;
8747
8748 if (unlikely(vmx->fail)) {
8749 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8750 vmcs_read32(VM_INSTRUCTION_ERROR));
8751 return true;
8752 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008753
David Matlackc9f04402017-08-01 14:00:40 -07008754 /*
8755 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008756 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8757 * Page). The CPU may write to these pages via their host
8758 * physical address while L2 is running, bypassing any
8759 * address-translation-based dirty tracking (e.g. EPT write
8760 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008761 *
8762 * Mark them dirty on every exit from L2 to prevent them from
8763 * getting out of sync with dirty tracking.
8764 */
8765 nested_mark_vmcs12_pages_dirty(vcpu);
8766
Jim Mattson4f350c62017-09-14 16:31:44 -07008767 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8768 vmcs_readl(EXIT_QUALIFICATION),
8769 vmx->idt_vectoring_info,
8770 intr_info,
8771 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8772 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008773
8774 switch (exit_reason) {
8775 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008776 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008777 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008778 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008779 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008780 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008781 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008782 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008783 else if (is_debug(intr_info) &&
8784 vcpu->guest_debug &
8785 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8786 return false;
8787 else if (is_breakpoint(intr_info) &&
8788 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8789 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008790 return vmcs12->exception_bitmap &
8791 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8792 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008793 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008794 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008795 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008796 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008797 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008798 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008799 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008800 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008801 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008802 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008803 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008804 case EXIT_REASON_HLT:
8805 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8806 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008807 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008808 case EXIT_REASON_INVLPG:
8809 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8810 case EXIT_REASON_RDPMC:
8811 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008812 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008813 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008814 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008815 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008816 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008817 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8818 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8819 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8820 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8821 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8822 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008823 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008824 /*
8825 * VMX instructions trap unconditionally. This allows L1 to
8826 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8827 */
Joe Perches1d804d02015-03-30 16:46:09 -07008828 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008829 case EXIT_REASON_CR_ACCESS:
8830 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8831 case EXIT_REASON_DR_ACCESS:
8832 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8833 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008834 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008835 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8836 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008837 case EXIT_REASON_MSR_READ:
8838 case EXIT_REASON_MSR_WRITE:
8839 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8840 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008841 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008842 case EXIT_REASON_MWAIT_INSTRUCTION:
8843 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008844 case EXIT_REASON_MONITOR_TRAP_FLAG:
8845 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008846 case EXIT_REASON_MONITOR_INSTRUCTION:
8847 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8848 case EXIT_REASON_PAUSE_INSTRUCTION:
8849 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8850 nested_cpu_has2(vmcs12,
8851 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8852 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008853 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008854 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008855 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008856 case EXIT_REASON_APIC_ACCESS:
8857 return nested_cpu_has2(vmcs12,
8858 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008859 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008860 case EXIT_REASON_EOI_INDUCED:
8861 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008862 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008863 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008864 /*
8865 * L0 always deals with the EPT violation. If nested EPT is
8866 * used, and the nested mmu code discovers that the address is
8867 * missing in the guest EPT table (EPT12), the EPT violation
8868 * will be injected with nested_ept_inject_page_fault()
8869 */
Joe Perches1d804d02015-03-30 16:46:09 -07008870 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008871 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008872 /*
8873 * L2 never uses directly L1's EPT, but rather L0's own EPT
8874 * table (shadow on EPT) or a merged EPT table that L0 built
8875 * (EPT on EPT). So any problems with the structure of the
8876 * table is L0's fault.
8877 */
Joe Perches1d804d02015-03-30 16:46:09 -07008878 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008879 case EXIT_REASON_INVPCID:
8880 return
8881 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8882 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008883 case EXIT_REASON_WBINVD:
8884 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8885 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008886 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008887 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8888 /*
8889 * This should never happen, since it is not possible to
8890 * set XSS to a non-zero value---neither in L1 nor in L2.
8891 * If if it were, XSS would have to be checked against
8892 * the XSS exit bitmap in vmcs12.
8893 */
8894 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008895 case EXIT_REASON_PREEMPTION_TIMER:
8896 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008897 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008898 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008899 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008900 case EXIT_REASON_VMFUNC:
8901 /* VM functions are emulated through L2->L0 vmexits. */
8902 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008903 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008904 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008905 }
8906}
8907
Paolo Bonzini7313c692017-07-27 10:31:25 +02008908static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8909{
8910 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8911
8912 /*
8913 * At this point, the exit interruption info in exit_intr_info
8914 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8915 * we need to query the in-kernel LAPIC.
8916 */
8917 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8918 if ((exit_intr_info &
8919 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8920 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8921 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8922 vmcs12->vm_exit_intr_error_code =
8923 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8924 }
8925
8926 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8927 vmcs_readl(EXIT_QUALIFICATION));
8928 return 1;
8929}
8930
Avi Kivity586f9602010-11-18 13:09:54 +02008931static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8932{
8933 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8934 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8935}
8936
Kai Huanga3eaa862015-11-04 13:46:05 +08008937static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008938{
Kai Huanga3eaa862015-11-04 13:46:05 +08008939 if (vmx->pml_pg) {
8940 __free_page(vmx->pml_pg);
8941 vmx->pml_pg = NULL;
8942 }
Kai Huang843e4332015-01-28 10:54:28 +08008943}
8944
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008945static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008946{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008947 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008948 u64 *pml_buf;
8949 u16 pml_idx;
8950
8951 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8952
8953 /* Do nothing if PML buffer is empty */
8954 if (pml_idx == (PML_ENTITY_NUM - 1))
8955 return;
8956
8957 /* PML index always points to next available PML buffer entity */
8958 if (pml_idx >= PML_ENTITY_NUM)
8959 pml_idx = 0;
8960 else
8961 pml_idx++;
8962
8963 pml_buf = page_address(vmx->pml_pg);
8964 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8965 u64 gpa;
8966
8967 gpa = pml_buf[pml_idx];
8968 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008969 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008970 }
8971
8972 /* reset PML index */
8973 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8974}
8975
8976/*
8977 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8978 * Called before reporting dirty_bitmap to userspace.
8979 */
8980static void kvm_flush_pml_buffers(struct kvm *kvm)
8981{
8982 int i;
8983 struct kvm_vcpu *vcpu;
8984 /*
8985 * We only need to kick vcpu out of guest mode here, as PML buffer
8986 * is flushed at beginning of all VMEXITs, and it's obvious that only
8987 * vcpus running in guest are possible to have unflushed GPAs in PML
8988 * buffer.
8989 */
8990 kvm_for_each_vcpu(i, vcpu, kvm)
8991 kvm_vcpu_kick(vcpu);
8992}
8993
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008994static void vmx_dump_sel(char *name, uint32_t sel)
8995{
8996 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008997 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008998 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8999 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9000 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9001}
9002
9003static void vmx_dump_dtsel(char *name, uint32_t limit)
9004{
9005 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9006 name, vmcs_read32(limit),
9007 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9008}
9009
9010static void dump_vmcs(void)
9011{
9012 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9013 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9014 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9015 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9016 u32 secondary_exec_control = 0;
9017 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009018 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009019 int i, n;
9020
9021 if (cpu_has_secondary_exec_ctrls())
9022 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9023
9024 pr_err("*** Guest State ***\n");
9025 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9026 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9027 vmcs_readl(CR0_GUEST_HOST_MASK));
9028 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9029 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9030 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9031 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9032 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9033 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009034 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9035 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9036 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9037 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009038 }
9039 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9040 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9041 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9042 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9043 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9044 vmcs_readl(GUEST_SYSENTER_ESP),
9045 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9046 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9047 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9048 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9049 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9050 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9051 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9052 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9053 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9054 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9055 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9056 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9057 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009058 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9059 efer, vmcs_read64(GUEST_IA32_PAT));
9060 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9061 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009062 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009063 if (cpu_has_load_perf_global_ctrl &&
9064 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009065 pr_err("PerfGlobCtl = 0x%016llx\n",
9066 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009067 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009068 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009069 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9070 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9071 vmcs_read32(GUEST_ACTIVITY_STATE));
9072 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9073 pr_err("InterruptStatus = %04x\n",
9074 vmcs_read16(GUEST_INTR_STATUS));
9075
9076 pr_err("*** Host State ***\n");
9077 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9078 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9079 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9080 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9081 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9082 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9083 vmcs_read16(HOST_TR_SELECTOR));
9084 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9085 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9086 vmcs_readl(HOST_TR_BASE));
9087 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9088 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9089 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9090 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9091 vmcs_readl(HOST_CR4));
9092 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9093 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9094 vmcs_read32(HOST_IA32_SYSENTER_CS),
9095 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9096 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009097 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9098 vmcs_read64(HOST_IA32_EFER),
9099 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009100 if (cpu_has_load_perf_global_ctrl &&
9101 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009102 pr_err("PerfGlobCtl = 0x%016llx\n",
9103 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009104
9105 pr_err("*** Control State ***\n");
9106 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9107 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9108 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9109 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9110 vmcs_read32(EXCEPTION_BITMAP),
9111 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9112 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9113 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9114 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9115 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9116 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9117 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9118 vmcs_read32(VM_EXIT_INTR_INFO),
9119 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9120 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9121 pr_err(" reason=%08x qualification=%016lx\n",
9122 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9123 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9124 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9125 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009126 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009127 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009128 pr_err("TSC Multiplier = 0x%016llx\n",
9129 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009130 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9131 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9132 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9133 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9134 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009135 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009136 n = vmcs_read32(CR3_TARGET_COUNT);
9137 for (i = 0; i + 1 < n; i += 4)
9138 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9139 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9140 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9141 if (i < n)
9142 pr_err("CR3 target%u=%016lx\n",
9143 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9144 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9145 pr_err("PLE Gap=%08x Window=%08x\n",
9146 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9147 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9148 pr_err("Virtual processor ID = 0x%04x\n",
9149 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9150}
9151
Avi Kivity6aa8b732006-12-10 02:21:36 -08009152/*
9153 * The guest has exited. See if we can fix it or if we need userspace
9154 * assistance.
9155 */
Avi Kivity851ba692009-08-24 11:10:17 +03009156static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009157{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009158 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009159 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009160 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009161
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009162 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9163
Kai Huang843e4332015-01-28 10:54:28 +08009164 /*
9165 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9166 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9167 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9168 * mode as if vcpus is in root mode, the PML buffer must has been
9169 * flushed already.
9170 */
9171 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009172 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009173
Mohammed Gamal80ced182009-09-01 12:48:18 +02009174 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009175 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009176 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009177
Paolo Bonzini7313c692017-07-27 10:31:25 +02009178 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9179 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009180
Mohammed Gamal51207022010-05-31 22:40:54 +03009181 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009182 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009183 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9184 vcpu->run->fail_entry.hardware_entry_failure_reason
9185 = exit_reason;
9186 return 0;
9187 }
9188
Avi Kivity29bd8a72007-09-10 17:27:03 +03009189 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009190 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9191 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009192 = vmcs_read32(VM_INSTRUCTION_ERROR);
9193 return 0;
9194 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009195
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009196 /*
9197 * Note:
9198 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9199 * delivery event since it indicates guest is accessing MMIO.
9200 * The vm-exit can be triggered again after return to guest that
9201 * will cause infinite loop.
9202 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009203 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009204 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009205 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009206 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009207 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9208 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9209 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009210 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009211 vcpu->run->internal.data[0] = vectoring_info;
9212 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009213 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9214 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9215 vcpu->run->internal.ndata++;
9216 vcpu->run->internal.data[3] =
9217 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9218 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009219 return 0;
9220 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009221
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009222 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009223 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9224 if (vmx_interrupt_allowed(vcpu)) {
9225 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9226 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9227 vcpu->arch.nmi_pending) {
9228 /*
9229 * This CPU don't support us in finding the end of an
9230 * NMI-blocked window if the guest runs with IRQs
9231 * disabled. So we pull the trigger after 1 s of
9232 * futile waiting, but inform the user about this.
9233 */
9234 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9235 "state on VCPU %d after 1 s timeout\n",
9236 __func__, vcpu->vcpu_id);
9237 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9238 }
9239 }
9240
Avi Kivity6aa8b732006-12-10 02:21:36 -08009241 if (exit_reason < kvm_vmx_max_exit_handlers
9242 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009243 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009244 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009245 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9246 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009247 kvm_queue_exception(vcpu, UD_VECTOR);
9248 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009249 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009250}
9251
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009252static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009253{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009254 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9255
9256 if (is_guest_mode(vcpu) &&
9257 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9258 return;
9259
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009260 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009261 vmcs_write32(TPR_THRESHOLD, 0);
9262 return;
9263 }
9264
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009265 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009266}
9267
Yang Zhang8d146952013-01-25 10:18:50 +08009268static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9269{
9270 u32 sec_exec_control;
9271
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009272 /* Postpone execution until vmcs01 is the current VMCS. */
9273 if (is_guest_mode(vcpu)) {
9274 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9275 return;
9276 }
9277
Wanpeng Lif6e90f92016-09-22 07:43:25 +08009278 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08009279 return;
9280
Paolo Bonzini35754c92015-07-29 12:05:37 +02009281 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009282 return;
9283
9284 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9285
9286 if (set) {
9287 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9288 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9289 } else {
9290 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9291 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009292 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009293 }
9294 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9295
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009296 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009297}
9298
Tang Chen38b99172014-09-24 15:57:54 +08009299static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9300{
9301 struct vcpu_vmx *vmx = to_vmx(vcpu);
9302
9303 /*
9304 * Currently we do not handle the nested case where L2 has an
9305 * APIC access page of its own; that page is still pinned.
9306 * Hence, we skip the case where the VCPU is in guest mode _and_
9307 * L1 prepared an APIC access page for L2.
9308 *
9309 * For the case where L1 and L2 share the same APIC access page
9310 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9311 * in the vmcs12), this function will only update either the vmcs01
9312 * or the vmcs02. If the former, the vmcs02 will be updated by
9313 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9314 * the next L2->L1 exit.
9315 */
9316 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07009317 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009318 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08009319 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009320 vmx_flush_tlb_ept_only(vcpu);
9321 }
Tang Chen38b99172014-09-24 15:57:54 +08009322}
9323
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009324static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009325{
9326 u16 status;
9327 u8 old;
9328
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009329 if (max_isr == -1)
9330 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009331
9332 status = vmcs_read16(GUEST_INTR_STATUS);
9333 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009334 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009335 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009336 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009337 vmcs_write16(GUEST_INTR_STATUS, status);
9338 }
9339}
9340
9341static void vmx_set_rvi(int vector)
9342{
9343 u16 status;
9344 u8 old;
9345
Wei Wang4114c272014-11-05 10:53:43 +08009346 if (vector == -1)
9347 vector = 0;
9348
Yang Zhangc7c9c562013-01-25 10:18:51 +08009349 status = vmcs_read16(GUEST_INTR_STATUS);
9350 old = (u8)status & 0xff;
9351 if ((u8)vector != old) {
9352 status &= ~0xff;
9353 status |= (u8)vector;
9354 vmcs_write16(GUEST_INTR_STATUS, status);
9355 }
9356}
9357
9358static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9359{
Liran Alon851c1a182017-12-24 18:12:56 +02009360 /*
9361 * When running L2, updating RVI is only relevant when
9362 * vmcs12 virtual-interrupt-delivery enabled.
9363 * However, it can be enabled only when L1 also
9364 * intercepts external-interrupts and in that case
9365 * we should not update vmcs02 RVI but instead intercept
9366 * interrupt. Therefore, do nothing when running L2.
9367 */
9368 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009369 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009370}
9371
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009372static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009373{
9374 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009375 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009376 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009377
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009378 WARN_ON(!vcpu->arch.apicv_active);
9379 if (pi_test_on(&vmx->pi_desc)) {
9380 pi_clear_on(&vmx->pi_desc);
9381 /*
9382 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9383 * But on x86 this is just a compiler barrier anyway.
9384 */
9385 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009386 max_irr_updated =
9387 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9388
9389 /*
9390 * If we are running L2 and L1 has a new pending interrupt
9391 * which can be injected, we should re-evaluate
9392 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009393 * If L1 intercepts external-interrupts, we should
9394 * exit from L2 to L1. Otherwise, interrupt should be
9395 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009396 */
Liran Alon851c1a182017-12-24 18:12:56 +02009397 if (is_guest_mode(vcpu) && max_irr_updated) {
9398 if (nested_exit_on_intr(vcpu))
9399 kvm_vcpu_exiting_guest_mode(vcpu);
9400 else
9401 kvm_make_request(KVM_REQ_EVENT, vcpu);
9402 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009403 } else {
9404 max_irr = kvm_lapic_find_highest_irr(vcpu);
9405 }
9406 vmx_hwapic_irr_update(vcpu, max_irr);
9407 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009408}
9409
Andrey Smetanin63086302015-11-10 15:36:32 +03009410static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009411{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009412 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009413 return;
9414
Yang Zhangc7c9c562013-01-25 10:18:51 +08009415 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9416 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9417 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9418 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9419}
9420
Paolo Bonzini967235d2016-12-19 14:03:45 +01009421static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9422{
9423 struct vcpu_vmx *vmx = to_vmx(vcpu);
9424
9425 pi_clear_on(&vmx->pi_desc);
9426 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9427}
9428
Avi Kivity51aa01d2010-07-20 14:31:20 +03009429static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009430{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009431 u32 exit_intr_info = 0;
9432 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009433
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009434 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9435 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009436 return;
9437
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009438 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9439 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9440 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009441
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009442 /* if exit due to PF check for async PF */
9443 if (is_page_fault(exit_intr_info))
9444 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9445
Andi Kleena0861c02009-06-08 17:37:09 +08009446 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009447 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9448 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009449 kvm_machine_check();
9450
Gleb Natapov20f65982009-05-11 13:35:55 +03009451 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009452 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009453 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009454 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009455 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009456 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009457}
Gleb Natapov20f65982009-05-11 13:35:55 +03009458
Yang Zhanga547c6d2013-04-11 19:25:10 +08009459static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9460{
9461 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9462
Yang Zhanga547c6d2013-04-11 19:25:10 +08009463 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9464 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9465 unsigned int vector;
9466 unsigned long entry;
9467 gate_desc *desc;
9468 struct vcpu_vmx *vmx = to_vmx(vcpu);
9469#ifdef CONFIG_X86_64
9470 unsigned long tmp;
9471#endif
9472
9473 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9474 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009475 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009476 asm volatile(
9477#ifdef CONFIG_X86_64
9478 "mov %%" _ASM_SP ", %[sp]\n\t"
9479 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9480 "push $%c[ss]\n\t"
9481 "push %[sp]\n\t"
9482#endif
9483 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009484 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009485 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009486 :
9487#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009488 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009489#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009490 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009491 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009492 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009493 [ss]"i"(__KERNEL_DS),
9494 [cs]"i"(__KERNEL_CS)
9495 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009496 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009497}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009498STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009499
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009500static bool vmx_has_high_real_mode_segbase(void)
9501{
9502 return enable_unrestricted_guest || emulate_invalid_guest_state;
9503}
9504
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009505static bool vmx_mpx_supported(void)
9506{
9507 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9508 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9509}
9510
Wanpeng Li55412b22014-12-02 19:21:30 +08009511static bool vmx_xsaves_supported(void)
9512{
9513 return vmcs_config.cpu_based_2nd_exec_ctrl &
9514 SECONDARY_EXEC_XSAVES;
9515}
9516
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009517static bool vmx_umip_emulated(void)
9518{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009519 return vmcs_config.cpu_based_2nd_exec_ctrl &
9520 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009521}
9522
Avi Kivity51aa01d2010-07-20 14:31:20 +03009523static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9524{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009525 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009526 bool unblock_nmi;
9527 u8 vector;
9528 bool idtv_info_valid;
9529
9530 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009531
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009532 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009533 if (vmx->loaded_vmcs->nmi_known_unmasked)
9534 return;
9535 /*
9536 * Can't use vmx->exit_intr_info since we're not sure what
9537 * the exit reason is.
9538 */
9539 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9540 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9541 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9542 /*
9543 * SDM 3: 27.7.1.2 (September 2008)
9544 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9545 * a guest IRET fault.
9546 * SDM 3: 23.2.2 (September 2008)
9547 * Bit 12 is undefined in any of the following cases:
9548 * If the VM exit sets the valid bit in the IDT-vectoring
9549 * information field.
9550 * If the VM exit is due to a double fault.
9551 */
9552 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9553 vector != DF_VECTOR && !idtv_info_valid)
9554 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9555 GUEST_INTR_STATE_NMI);
9556 else
9557 vmx->loaded_vmcs->nmi_known_unmasked =
9558 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9559 & GUEST_INTR_STATE_NMI);
9560 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9561 vmx->loaded_vmcs->vnmi_blocked_time +=
9562 ktime_to_ns(ktime_sub(ktime_get(),
9563 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009564}
9565
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009566static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009567 u32 idt_vectoring_info,
9568 int instr_len_field,
9569 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009570{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009571 u8 vector;
9572 int type;
9573 bool idtv_info_valid;
9574
9575 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009576
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009577 vcpu->arch.nmi_injected = false;
9578 kvm_clear_exception_queue(vcpu);
9579 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009580
9581 if (!idtv_info_valid)
9582 return;
9583
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009584 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009585
Avi Kivity668f6122008-07-02 09:28:55 +03009586 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9587 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009588
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009589 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009590 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009591 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009592 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009593 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009594 * Clear bit "block by NMI" before VM entry if a NMI
9595 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009596 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009597 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009598 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009599 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009600 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009601 /* fall through */
9602 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009603 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009604 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009605 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009606 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009607 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009608 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009609 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009610 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009611 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009612 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009613 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009614 break;
9615 default:
9616 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009617 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009618}
9619
Avi Kivity83422e12010-07-20 14:43:23 +03009620static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9621{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009622 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009623 VM_EXIT_INSTRUCTION_LEN,
9624 IDT_VECTORING_ERROR_CODE);
9625}
9626
Avi Kivityb463a6f2010-07-20 15:06:17 +03009627static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9628{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009629 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009630 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9631 VM_ENTRY_INSTRUCTION_LEN,
9632 VM_ENTRY_EXCEPTION_ERROR_CODE);
9633
9634 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9635}
9636
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009637static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9638{
9639 int i, nr_msrs;
9640 struct perf_guest_switch_msr *msrs;
9641
9642 msrs = perf_guest_get_msrs(&nr_msrs);
9643
9644 if (!msrs)
9645 return;
9646
9647 for (i = 0; i < nr_msrs; i++)
9648 if (msrs[i].host == msrs[i].guest)
9649 clear_atomic_switch_msr(vmx, msrs[i].msr);
9650 else
9651 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9652 msrs[i].host);
9653}
9654
Jiang Biao33365e72016-11-03 15:03:37 +08009655static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009656{
9657 struct vcpu_vmx *vmx = to_vmx(vcpu);
9658 u64 tscl;
9659 u32 delta_tsc;
9660
9661 if (vmx->hv_deadline_tsc == -1)
9662 return;
9663
9664 tscl = rdtsc();
9665 if (vmx->hv_deadline_tsc > tscl)
9666 /* sure to be 32 bit only because checked on set_hv_timer */
9667 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9668 cpu_preemption_timer_multi);
9669 else
9670 delta_tsc = 0;
9671
9672 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9673}
9674
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009675static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009676{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009677 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009678 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009679
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009680 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009681 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009682 vmx->loaded_vmcs->soft_vnmi_blocked))
9683 vmx->loaded_vmcs->entry_time = ktime_get();
9684
Avi Kivity104f2262010-11-18 13:12:52 +02009685 /* Don't enter VMX if guest state is invalid, let the exit handler
9686 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009687 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009688 return;
9689
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009690 if (vmx->ple_window_dirty) {
9691 vmx->ple_window_dirty = false;
9692 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9693 }
9694
Abel Gordon012f83c2013-04-18 14:39:25 +03009695 if (vmx->nested.sync_shadow_vmcs) {
9696 copy_vmcs12_to_shadow(vmx);
9697 vmx->nested.sync_shadow_vmcs = false;
9698 }
9699
Avi Kivity104f2262010-11-18 13:12:52 +02009700 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9701 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9702 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9703 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9704
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009705 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009706 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009707 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009708 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009709 }
9710
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009711 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009712 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009713 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009714 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009715 }
9716
Avi Kivity104f2262010-11-18 13:12:52 +02009717 /* When single-stepping over STI and MOV SS, we must clear the
9718 * corresponding interruptibility bits in the guest state. Otherwise
9719 * vmentry fails as it then expects bit 14 (BS) in pending debug
9720 * exceptions being set, but that's not correct for the guest debugging
9721 * case. */
9722 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9723 vmx_set_interrupt_shadow(vcpu, 0);
9724
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009725 if (static_cpu_has(X86_FEATURE_PKU) &&
9726 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9727 vcpu->arch.pkru != vmx->host_pkru)
9728 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009729
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009730 atomic_switch_perf_msrs(vmx);
9731
Yunhong Jiang64672c92016-06-13 14:19:59 -07009732 vmx_arm_hv_timer(vcpu);
9733
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009734 /*
9735 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9736 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9737 * is no need to worry about the conditional branch over the wrmsr
9738 * being speculatively taken.
9739 */
9740 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009741 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009742
Nadav Har'Eld462b812011-05-24 15:26:10 +03009743 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009744
9745 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9746 (unsigned long)&current_evmcs->host_rsp : 0;
9747
Avi Kivity104f2262010-11-18 13:12:52 +02009748 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009749 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009750 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9751 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9752 "push %%" _ASM_CX " \n\t"
9753 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009754 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009755 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009756 /* Avoid VMWRITE when Enlightened VMCS is in use */
9757 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9758 "jz 2f \n\t"
9759 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9760 "jmp 1f \n\t"
9761 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009762 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009763 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009764 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009765 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9766 "mov %%cr2, %%" _ASM_DX " \n\t"
9767 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009768 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009769 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009770 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009771 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009772 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009773 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009774 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9775 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9776 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9777 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9778 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9779 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009780#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009781 "mov %c[r8](%0), %%r8 \n\t"
9782 "mov %c[r9](%0), %%r9 \n\t"
9783 "mov %c[r10](%0), %%r10 \n\t"
9784 "mov %c[r11](%0), %%r11 \n\t"
9785 "mov %c[r12](%0), %%r12 \n\t"
9786 "mov %c[r13](%0), %%r13 \n\t"
9787 "mov %c[r14](%0), %%r14 \n\t"
9788 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009789#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009790 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009791
Avi Kivity6aa8b732006-12-10 02:21:36 -08009792 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009793 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009794 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009795 "jmp 2f \n\t"
9796 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9797 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009798 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009799 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009800 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009801 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009802 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9803 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9804 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9805 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9806 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9807 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9808 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009809#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009810 "mov %%r8, %c[r8](%0) \n\t"
9811 "mov %%r9, %c[r9](%0) \n\t"
9812 "mov %%r10, %c[r10](%0) \n\t"
9813 "mov %%r11, %c[r11](%0) \n\t"
9814 "mov %%r12, %c[r12](%0) \n\t"
9815 "mov %%r13, %c[r13](%0) \n\t"
9816 "mov %%r14, %c[r14](%0) \n\t"
9817 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009818 "xor %%r8d, %%r8d \n\t"
9819 "xor %%r9d, %%r9d \n\t"
9820 "xor %%r10d, %%r10d \n\t"
9821 "xor %%r11d, %%r11d \n\t"
9822 "xor %%r12d, %%r12d \n\t"
9823 "xor %%r13d, %%r13d \n\t"
9824 "xor %%r14d, %%r14d \n\t"
9825 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009826#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009827 "mov %%cr2, %%" _ASM_AX " \n\t"
9828 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009829
Jim Mattson0cb5b302018-01-03 14:31:38 -08009830 "xor %%eax, %%eax \n\t"
9831 "xor %%ebx, %%ebx \n\t"
9832 "xor %%esi, %%esi \n\t"
9833 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009834 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009835 ".pushsection .rodata \n\t"
9836 ".global vmx_return \n\t"
9837 "vmx_return: " _ASM_PTR " 2b \n\t"
9838 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009839 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009840 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009841 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009842 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009843 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9844 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9845 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9846 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9847 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9848 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9849 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009850#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009851 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9852 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9853 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9854 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9855 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9856 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9857 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9858 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009859#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009860 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9861 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009862 : "cc", "memory"
9863#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009864 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009865 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009866#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009867 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009868#endif
9869 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009870
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009871 /*
9872 * We do not use IBRS in the kernel. If this vCPU has used the
9873 * SPEC_CTRL MSR it may have left it on; save the value and
9874 * turn it off. This is much more efficient than blindly adding
9875 * it to the atomic save/restore list. Especially as the former
9876 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9877 *
9878 * For non-nested case:
9879 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9880 * save it.
9881 *
9882 * For nested case:
9883 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9884 * save it.
9885 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009886 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009887 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009888
9889 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009890 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009891
David Woodhouse117cc7a2018-01-12 11:11:27 +00009892 /* Eliminate branch target predictions from guest mode */
9893 vmexit_fill_RSB();
9894
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009895 /* All fields are clean at this point */
9896 if (static_branch_unlikely(&enable_evmcs))
9897 current_evmcs->hv_clean_fields |=
9898 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9899
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009900 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009901 if (vmx->host_debugctlmsr)
9902 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009903
Avi Kivityaa67f602012-08-01 16:48:03 +03009904#ifndef CONFIG_X86_64
9905 /*
9906 * The sysexit path does not restore ds/es, so we must set them to
9907 * a reasonable value ourselves.
9908 *
9909 * We can't defer this to vmx_load_host_state() since that function
9910 * may be executed in interrupt context, which saves and restore segments
9911 * around it, nullifying its effect.
9912 */
9913 loadsegment(ds, __USER_DS);
9914 loadsegment(es, __USER_DS);
9915#endif
9916
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009917 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009918 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009919 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009920 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009921 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009922 vcpu->arch.regs_dirty = 0;
9923
Gleb Natapove0b890d2013-09-25 12:51:33 +03009924 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009925 * eager fpu is enabled if PKEY is supported and CR4 is switched
9926 * back on host, so it is safe to read guest PKRU from current
9927 * XSAVE.
9928 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009929 if (static_cpu_has(X86_FEATURE_PKU) &&
9930 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9931 vcpu->arch.pkru = __read_pkru();
9932 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009933 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009934 }
9935
Gleb Natapove0b890d2013-09-25 12:51:33 +03009936 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009937 vmx->idt_vectoring_info = 0;
9938
9939 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9940 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9941 return;
9942
9943 vmx->loaded_vmcs->launched = 1;
9944 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009945
Avi Kivity51aa01d2010-07-20 14:31:20 +03009946 vmx_complete_atomic_exit(vmx);
9947 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009948 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009949}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009950STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009951
Sean Christopherson434a1e92018-03-20 12:17:18 -07009952static struct kvm *vmx_vm_alloc(void)
9953{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009954 struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
9955 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07009956}
9957
9958static void vmx_vm_free(struct kvm *kvm)
9959{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009960 kfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07009961}
9962
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009963static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009964{
9965 struct vcpu_vmx *vmx = to_vmx(vcpu);
9966 int cpu;
9967
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009968 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009969 return;
9970
9971 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009972 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009973 vmx_vcpu_put(vcpu);
9974 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009975 put_cpu();
9976}
9977
Jim Mattson2f1fe812016-07-08 15:36:06 -07009978/*
9979 * Ensure that the current vmcs of the logical processor is the
9980 * vmcs01 of the vcpu before calling free_nested().
9981 */
9982static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9983{
9984 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009985
Christoffer Dallec7660c2017-12-04 21:35:23 +01009986 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009987 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009988 free_nested(vmx);
9989 vcpu_put(vcpu);
9990}
9991
Avi Kivity6aa8b732006-12-10 02:21:36 -08009992static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9993{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009994 struct vcpu_vmx *vmx = to_vmx(vcpu);
9995
Kai Huang843e4332015-01-28 10:54:28 +08009996 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009997 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009998 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009999 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010000 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010001 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010002 kfree(vmx->guest_msrs);
10003 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010004 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010005}
10006
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010007static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010008{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010009 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010010 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010011 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010012 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010013
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010014 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010015 return ERR_PTR(-ENOMEM);
10016
Wanpeng Li991e7a02015-09-16 17:30:05 +080010017 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010018
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010019 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10020 if (err)
10021 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010022
Peter Feiner4e595162016-07-07 14:49:58 -070010023 err = -ENOMEM;
10024
10025 /*
10026 * If PML is turned on, failure on enabling PML just results in failure
10027 * of creating the vcpu, therefore we can simplify PML logic (by
10028 * avoiding dealing with cases, such as enabling PML partially on vcpus
10029 * for the guest, etc.
10030 */
10031 if (enable_pml) {
10032 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10033 if (!vmx->pml_pg)
10034 goto uninit_vcpu;
10035 }
10036
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010037 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010038 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10039 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010040
Peter Feiner4e595162016-07-07 14:49:58 -070010041 if (!vmx->guest_msrs)
10042 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010043
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010044 err = alloc_loaded_vmcs(&vmx->vmcs01);
10045 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010046 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010047
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010048 msr_bitmap = vmx->vmcs01.msr_bitmap;
10049 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10050 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10051 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10052 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10053 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10054 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10055 vmx->msr_bitmap_mode = 0;
10056
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010057 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010058 cpu = get_cpu();
10059 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010060 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010061 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010062 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010063 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010064 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010065 err = alloc_apic_access_page(kvm);
10066 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010067 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010068 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010069
Sean Christophersone90008d2018-03-05 12:04:37 -080010070 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010071 err = init_rmode_identity_map(kvm);
10072 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010073 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010074 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010075
Wanpeng Li5c614b32015-10-13 09:18:36 -070010076 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010077 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10078 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010079 vmx->nested.vpid02 = allocate_vpid();
10080 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010081
Wincy Van705699a2015-02-03 23:58:17 +080010082 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010083 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010084
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010085 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10086
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010087 /*
10088 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10089 * or POSTED_INTR_WAKEUP_VECTOR.
10090 */
10091 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10092 vmx->pi_desc.sn = 1;
10093
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010094 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010095
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010096free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010097 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010098 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010099free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010100 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010101free_pml:
10102 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010103uninit_vcpu:
10104 kvm_vcpu_uninit(&vmx->vcpu);
10105free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010106 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010107 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010108 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010109}
10110
Wanpeng Lib31c1142018-03-12 04:53:04 -070010111static int vmx_vm_init(struct kvm *kvm)
10112{
10113 if (!ple_gap)
10114 kvm->arch.pause_in_guest = true;
10115 return 0;
10116}
10117
Yang, Sheng002c7f72007-07-31 14:23:01 +030010118static void __init vmx_check_processor_compat(void *rtn)
10119{
10120 struct vmcs_config vmcs_conf;
10121
10122 *(int *)rtn = 0;
10123 if (setup_vmcs_config(&vmcs_conf) < 0)
10124 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010125 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010126 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10127 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10128 smp_processor_id());
10129 *(int *)rtn = -EIO;
10130 }
10131}
10132
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010133static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010134{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010135 u8 cache;
10136 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010137
Sheng Yang522c68c2009-04-27 20:35:43 +080010138 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010139 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010140 * 2. EPT with VT-d:
10141 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010142 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010143 * b. VT-d with snooping control feature: snooping control feature of
10144 * VT-d engine can guarantee the cache correctness. Just set it
10145 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010146 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010147 * consistent with host MTRR
10148 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010149 if (is_mmio) {
10150 cache = MTRR_TYPE_UNCACHABLE;
10151 goto exit;
10152 }
10153
10154 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010155 ipat = VMX_EPT_IPAT_BIT;
10156 cache = MTRR_TYPE_WRBACK;
10157 goto exit;
10158 }
10159
10160 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10161 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010162 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010163 cache = MTRR_TYPE_WRBACK;
10164 else
10165 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010166 goto exit;
10167 }
10168
Xiao Guangrongff536042015-06-15 16:55:22 +080010169 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010170
10171exit:
10172 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010173}
10174
Sheng Yang17cc3932010-01-05 19:02:27 +080010175static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010176{
Sheng Yang878403b2010-01-05 19:02:29 +080010177 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10178 return PT_DIRECTORY_LEVEL;
10179 else
10180 /* For shadow and EPT supported 1GB page */
10181 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010182}
10183
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010184static void vmcs_set_secondary_exec_control(u32 new_ctl)
10185{
10186 /*
10187 * These bits in the secondary execution controls field
10188 * are dynamic, the others are mostly based on the hypervisor
10189 * architecture and the guest's CPUID. Do not touch the
10190 * dynamic bits.
10191 */
10192 u32 mask =
10193 SECONDARY_EXEC_SHADOW_VMCS |
10194 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010195 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10196 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010197
10198 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10199
10200 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10201 (new_ctl & ~mask) | (cur_ctl & mask));
10202}
10203
David Matlack8322ebb2016-11-29 18:14:09 -080010204/*
10205 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10206 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10207 */
10208static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10209{
10210 struct vcpu_vmx *vmx = to_vmx(vcpu);
10211 struct kvm_cpuid_entry2 *entry;
10212
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010213 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10214 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010215
10216#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10217 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010218 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010219} while (0)
10220
10221 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10222 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10223 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10224 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10225 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10226 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10227 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10228 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10229 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10230 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10231 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10232 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10233 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10234 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10235 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10236
10237 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10238 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10239 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10240 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10241 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010242 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010243
10244#undef cr4_fixed1_update
10245}
10246
Sheng Yang0e851882009-12-18 16:48:46 +080010247static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10248{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010249 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010250
Paolo Bonzini80154d72017-08-24 13:55:35 +020010251 if (cpu_has_secondary_exec_ctrls()) {
10252 vmx_compute_secondary_exec_control(vmx);
10253 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010254 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010255
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010256 if (nested_vmx_allowed(vcpu))
10257 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10258 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10259 else
10260 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10261 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010262
10263 if (nested_vmx_allowed(vcpu))
10264 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010265}
10266
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010267static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10268{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010269 if (func == 1 && nested)
10270 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010271}
10272
Yang Zhang25d92082013-08-06 12:00:32 +030010273static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10274 struct x86_exception *fault)
10275{
Jan Kiszka533558b2014-01-04 18:47:20 +010010276 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010277 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010278 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010279 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010280
Bandan Dasc5f983f2017-05-05 15:25:14 -040010281 if (vmx->nested.pml_full) {
10282 exit_reason = EXIT_REASON_PML_FULL;
10283 vmx->nested.pml_full = false;
10284 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10285 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010286 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010287 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010288 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010289
10290 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010291 vmcs12->guest_physical_address = fault->address;
10292}
10293
Peter Feiner995f00a2017-06-30 17:26:32 -070010294static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10295{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010296 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010297}
10298
Nadav Har'El155a97a2013-08-05 11:07:16 +030010299/* Callbacks for nested_ept_init_mmu_context: */
10300
10301static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10302{
10303 /* return the page table to be shadowed - in our case, EPT12 */
10304 return get_vmcs12(vcpu)->ept_pointer;
10305}
10306
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010307static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010308{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010309 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010310 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010311 return 1;
10312
10313 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010314 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010315 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010316 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010317 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010318 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10319 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10320 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10321
10322 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010323 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010324}
10325
10326static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10327{
10328 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10329}
10330
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010331static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10332 u16 error_code)
10333{
10334 bool inequality, bit;
10335
10336 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10337 inequality =
10338 (error_code & vmcs12->page_fault_error_code_mask) !=
10339 vmcs12->page_fault_error_code_match;
10340 return inequality ^ bit;
10341}
10342
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010343static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10344 struct x86_exception *fault)
10345{
10346 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10347
10348 WARN_ON(!is_guest_mode(vcpu));
10349
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010350 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10351 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010352 vmcs12->vm_exit_intr_error_code = fault->error_code;
10353 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10354 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10355 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10356 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010357 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010358 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010359 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010360}
10361
Paolo Bonzinic9923842017-12-13 14:16:30 +010010362static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10363 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010364
10365static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010366 struct vmcs12 *vmcs12)
10367{
10368 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010369 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010370 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010371
10372 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010373 /*
10374 * Translate L1 physical address to host physical
10375 * address for vmcs02. Keep the page pinned, so this
10376 * physical address remains valid. We keep a reference
10377 * to it so we can release it later.
10378 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010379 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010380 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010381 vmx->nested.apic_access_page = NULL;
10382 }
10383 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010384 /*
10385 * If translation failed, no matter: This feature asks
10386 * to exit when accessing the given address, and if it
10387 * can never be accessed, this feature won't do
10388 * anything anyway.
10389 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010390 if (!is_error_page(page)) {
10391 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010392 hpa = page_to_phys(vmx->nested.apic_access_page);
10393 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10394 } else {
10395 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10396 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10397 }
10398 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10399 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10400 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10401 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10402 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010403 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010404
10405 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010406 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010407 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010408 vmx->nested.virtual_apic_page = NULL;
10409 }
10410 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010411
10412 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010413 * If translation failed, VM entry will fail because
10414 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10415 * Failing the vm entry is _not_ what the processor
10416 * does but it's basically the only possibility we
10417 * have. We could still enter the guest if CR8 load
10418 * exits are enabled, CR8 store exits are enabled, and
10419 * virtualize APIC access is disabled; in this case
10420 * the processor would never use the TPR shadow and we
10421 * could simply clear the bit from the execution
10422 * control. But such a configuration is useless, so
10423 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010424 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010425 if (!is_error_page(page)) {
10426 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010427 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10428 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10429 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010430 }
10431
Wincy Van705699a2015-02-03 23:58:17 +080010432 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010433 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10434 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010435 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010436 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010437 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010438 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10439 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010440 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010441 vmx->nested.pi_desc_page = page;
10442 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010443 vmx->nested.pi_desc =
10444 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10445 (unsigned long)(vmcs12->posted_intr_desc_addr &
10446 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010447 vmcs_write64(POSTED_INTR_DESC_ADDR,
10448 page_to_phys(vmx->nested.pi_desc_page) +
10449 (unsigned long)(vmcs12->posted_intr_desc_addr &
10450 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010451 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010452 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010453 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10454 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010455 else
10456 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10457 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010458}
10459
Jan Kiszkaf41245002014-03-07 20:03:13 +010010460static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10461{
10462 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10463 struct vcpu_vmx *vmx = to_vmx(vcpu);
10464
10465 if (vcpu->arch.virtual_tsc_khz == 0)
10466 return;
10467
10468 /* Make sure short timeouts reliably trigger an immediate vmexit.
10469 * hrtimer_start does not guarantee this. */
10470 if (preemption_timeout <= 1) {
10471 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10472 return;
10473 }
10474
10475 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10476 preemption_timeout *= 1000000;
10477 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10478 hrtimer_start(&vmx->nested.preemption_timer,
10479 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10480}
10481
Jim Mattson56a20512017-07-06 16:33:06 -070010482static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10483 struct vmcs12 *vmcs12)
10484{
10485 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10486 return 0;
10487
10488 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10489 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10490 return -EINVAL;
10491
10492 return 0;
10493}
10494
Wincy Van3af18d92015-02-03 23:49:31 +080010495static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10496 struct vmcs12 *vmcs12)
10497{
Wincy Van3af18d92015-02-03 23:49:31 +080010498 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10499 return 0;
10500
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010501 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010502 return -EINVAL;
10503
10504 return 0;
10505}
10506
Jim Mattson712b12d2017-08-24 13:24:47 -070010507static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10508 struct vmcs12 *vmcs12)
10509{
10510 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10511 return 0;
10512
10513 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10514 return -EINVAL;
10515
10516 return 0;
10517}
10518
Wincy Van3af18d92015-02-03 23:49:31 +080010519/*
10520 * Merge L0's and L1's MSR bitmap, return false to indicate that
10521 * we do not use the hardware.
10522 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010523static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10524 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010525{
Wincy Van82f0dd42015-02-03 23:57:18 +080010526 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010527 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010528 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010529 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010530 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010531 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010532 *
10533 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10534 * ensures that we do not accidentally generate an L02 MSR bitmap
10535 * from the L12 MSR bitmap that is too permissive.
10536 * 2. That L1 or L2s have actually used the MSR. This avoids
10537 * unnecessarily merging of the bitmap if the MSR is unused. This
10538 * works properly because we only update the L01 MSR bitmap lazily.
10539 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10540 * updated to reflect this when L1 (or its L2s) actually write to
10541 * the MSR.
10542 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010543 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10544 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010545
Paolo Bonzinic9923842017-12-13 14:16:30 +010010546 /* Nothing to do if the MSR bitmap is not in use. */
10547 if (!cpu_has_vmx_msr_bitmap() ||
10548 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10549 return false;
10550
Ashok Raj15d45072018-02-01 22:59:43 +010010551 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010552 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010553 return false;
10554
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010555 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10556 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010557 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010558
Radim Krčmářd048c092016-08-08 20:16:22 +020010559 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010560 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10561 /*
10562 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10563 * just lets the processor take the value from the virtual-APIC page;
10564 * take those 256 bits directly from the L1 bitmap.
10565 */
10566 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10567 unsigned word = msr / BITS_PER_LONG;
10568 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10569 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010570 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010571 } else {
10572 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10573 unsigned word = msr / BITS_PER_LONG;
10574 msr_bitmap_l0[word] = ~0;
10575 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10576 }
10577 }
10578
10579 nested_vmx_disable_intercept_for_msr(
10580 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010581 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010582 MSR_TYPE_W);
10583
10584 if (nested_cpu_has_vid(vmcs12)) {
10585 nested_vmx_disable_intercept_for_msr(
10586 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010587 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010588 MSR_TYPE_W);
10589 nested_vmx_disable_intercept_for_msr(
10590 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010591 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010592 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010593 }
Ashok Raj15d45072018-02-01 22:59:43 +010010594
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010595 if (spec_ctrl)
10596 nested_vmx_disable_intercept_for_msr(
10597 msr_bitmap_l1, msr_bitmap_l0,
10598 MSR_IA32_SPEC_CTRL,
10599 MSR_TYPE_R | MSR_TYPE_W);
10600
Ashok Raj15d45072018-02-01 22:59:43 +010010601 if (pred_cmd)
10602 nested_vmx_disable_intercept_for_msr(
10603 msr_bitmap_l1, msr_bitmap_l0,
10604 MSR_IA32_PRED_CMD,
10605 MSR_TYPE_W);
10606
Wincy Vanf2b93282015-02-03 23:56:03 +080010607 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010608 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010609
10610 return true;
10611}
10612
10613static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10614 struct vmcs12 *vmcs12)
10615{
Wincy Van82f0dd42015-02-03 23:57:18 +080010616 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010617 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010618 !nested_cpu_has_vid(vmcs12) &&
10619 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010620 return 0;
10621
10622 /*
10623 * If virtualize x2apic mode is enabled,
10624 * virtualize apic access must be disabled.
10625 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010626 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10627 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010628 return -EINVAL;
10629
Wincy Van608406e2015-02-03 23:57:51 +080010630 /*
10631 * If virtual interrupt delivery is enabled,
10632 * we must exit on external interrupts.
10633 */
10634 if (nested_cpu_has_vid(vmcs12) &&
10635 !nested_exit_on_intr(vcpu))
10636 return -EINVAL;
10637
Wincy Van705699a2015-02-03 23:58:17 +080010638 /*
10639 * bits 15:8 should be zero in posted_intr_nv,
10640 * the descriptor address has been already checked
10641 * in nested_get_vmcs12_pages.
10642 */
10643 if (nested_cpu_has_posted_intr(vmcs12) &&
10644 (!nested_cpu_has_vid(vmcs12) ||
10645 !nested_exit_intr_ack_set(vcpu) ||
10646 vmcs12->posted_intr_nv & 0xff00))
10647 return -EINVAL;
10648
Wincy Vanf2b93282015-02-03 23:56:03 +080010649 /* tpr shadow is needed by all apicv features. */
10650 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10651 return -EINVAL;
10652
10653 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010654}
10655
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010656static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10657 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010658 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010659{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010660 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010661 u64 count, addr;
10662
10663 if (vmcs12_read_any(vcpu, count_field, &count) ||
10664 vmcs12_read_any(vcpu, addr_field, &addr)) {
10665 WARN_ON(1);
10666 return -EINVAL;
10667 }
10668 if (count == 0)
10669 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010670 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010671 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10672 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010673 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010674 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10675 addr_field, maxphyaddr, count, addr);
10676 return -EINVAL;
10677 }
10678 return 0;
10679}
10680
10681static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10682 struct vmcs12 *vmcs12)
10683{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010684 if (vmcs12->vm_exit_msr_load_count == 0 &&
10685 vmcs12->vm_exit_msr_store_count == 0 &&
10686 vmcs12->vm_entry_msr_load_count == 0)
10687 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010688 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010689 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010690 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010691 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010692 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010693 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010694 return -EINVAL;
10695 return 0;
10696}
10697
Bandan Dasc5f983f2017-05-05 15:25:14 -040010698static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10699 struct vmcs12 *vmcs12)
10700{
10701 u64 address = vmcs12->pml_address;
10702 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10703
10704 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10705 if (!nested_cpu_has_ept(vmcs12) ||
10706 !IS_ALIGNED(address, 4096) ||
10707 address >> maxphyaddr)
10708 return -EINVAL;
10709 }
10710
10711 return 0;
10712}
10713
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010714static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10715 struct vmx_msr_entry *e)
10716{
10717 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010718 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010719 return -EINVAL;
10720 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10721 e->index == MSR_IA32_UCODE_REV)
10722 return -EINVAL;
10723 if (e->reserved != 0)
10724 return -EINVAL;
10725 return 0;
10726}
10727
10728static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10729 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010730{
10731 if (e->index == MSR_FS_BASE ||
10732 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010733 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10734 nested_vmx_msr_check_common(vcpu, e))
10735 return -EINVAL;
10736 return 0;
10737}
10738
10739static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10740 struct vmx_msr_entry *e)
10741{
10742 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10743 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010744 return -EINVAL;
10745 return 0;
10746}
10747
10748/*
10749 * Load guest's/host's msr at nested entry/exit.
10750 * return 0 for success, entry index for failure.
10751 */
10752static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10753{
10754 u32 i;
10755 struct vmx_msr_entry e;
10756 struct msr_data msr;
10757
10758 msr.host_initiated = false;
10759 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010760 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10761 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010762 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010763 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10764 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010765 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010766 }
10767 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010768 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010769 "%s check failed (%u, 0x%x, 0x%x)\n",
10770 __func__, i, e.index, e.reserved);
10771 goto fail;
10772 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010773 msr.index = e.index;
10774 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010775 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010776 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010777 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10778 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010779 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010780 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010781 }
10782 return 0;
10783fail:
10784 return i + 1;
10785}
10786
10787static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10788{
10789 u32 i;
10790 struct vmx_msr_entry e;
10791
10792 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010793 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010794 if (kvm_vcpu_read_guest(vcpu,
10795 gpa + i * sizeof(e),
10796 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010797 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010798 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10799 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010800 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010801 }
10802 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010803 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010804 "%s check failed (%u, 0x%x, 0x%x)\n",
10805 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010806 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010807 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010808 msr_info.host_initiated = false;
10809 msr_info.index = e.index;
10810 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010811 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010812 "%s cannot read MSR (%u, 0x%x)\n",
10813 __func__, i, e.index);
10814 return -EINVAL;
10815 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010816 if (kvm_vcpu_write_guest(vcpu,
10817 gpa + i * sizeof(e) +
10818 offsetof(struct vmx_msr_entry, value),
10819 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010820 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010821 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010822 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010823 return -EINVAL;
10824 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010825 }
10826 return 0;
10827}
10828
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010829static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10830{
10831 unsigned long invalid_mask;
10832
10833 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10834 return (val & invalid_mask) == 0;
10835}
10836
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010837/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010838 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10839 * emulating VM entry into a guest with EPT enabled.
10840 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10841 * is assigned to entry_failure_code on failure.
10842 */
10843static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010844 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010845{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010846 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010847 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010848 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10849 return 1;
10850 }
10851
10852 /*
10853 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10854 * must not be dereferenced.
10855 */
10856 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10857 !nested_ept) {
10858 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10859 *entry_failure_code = ENTRY_FAIL_PDPTE;
10860 return 1;
10861 }
10862 }
10863
10864 vcpu->arch.cr3 = cr3;
10865 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10866 }
10867
10868 kvm_mmu_reset_context(vcpu);
10869 return 0;
10870}
10871
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010872static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10873 bool from_vmentry)
10874{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010875 struct vcpu_vmx *vmx = to_vmx(vcpu);
10876
10877 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10878 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10879 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10880 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10881 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10882 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10883 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10884 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10885 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10886 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10887 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10888 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10889 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10890 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10891 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10892 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10893 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10894 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10895 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10896 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10897 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10898 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10899 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10900 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10901 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10902 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10903 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10904 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10905 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10906 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10907 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010908
10909 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10910 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10911 vmcs12->guest_pending_dbg_exceptions);
10912 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10913 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10914
10915 if (nested_cpu_has_xsaves(vmcs12))
10916 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10917 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10918
10919 if (cpu_has_vmx_posted_intr())
10920 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10921
10922 /*
10923 * Whether page-faults are trapped is determined by a combination of
10924 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10925 * If enable_ept, L0 doesn't care about page faults and we should
10926 * set all of these to L1's desires. However, if !enable_ept, L0 does
10927 * care about (at least some) page faults, and because it is not easy
10928 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10929 * to exit on each and every L2 page fault. This is done by setting
10930 * MASK=MATCH=0 and (see below) EB.PF=1.
10931 * Note that below we don't need special code to set EB.PF beyond the
10932 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10933 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10934 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10935 */
10936 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10937 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10938 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10939 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10940
10941 /* All VMFUNCs are currently emulated through L0 vmexits. */
10942 if (cpu_has_vmx_vmfunc())
10943 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10944
10945 if (cpu_has_vmx_apicv()) {
10946 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10947 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10948 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10949 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10950 }
10951
10952 /*
10953 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10954 * Some constant fields are set here by vmx_set_constant_host_state().
10955 * Other fields are different per CPU, and will be set later when
10956 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10957 */
10958 vmx_set_constant_host_state(vmx);
10959
10960 /*
10961 * Set the MSR load/store lists to match L0's settings.
10962 */
10963 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10964 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10965 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10966 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10967 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10968
10969 set_cr4_guest_host_mask(vmx);
10970
10971 if (vmx_mpx_supported())
10972 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10973
10974 if (enable_vpid) {
10975 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10976 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10977 else
10978 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10979 }
10980
10981 /*
10982 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10983 */
10984 if (enable_ept) {
10985 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10986 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10987 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10988 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10989 }
Radim Krčmář80132f42018-02-02 18:26:58 +010010990
10991 if (cpu_has_vmx_msr_bitmap())
10992 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010993}
10994
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010995/*
10996 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10997 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010998 * 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 +030010999 * guest in a way that will both be appropriate to L1's requests, and our
11000 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11001 * function also has additional necessary side-effects, like setting various
11002 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011003 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11004 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011005 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011006static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011007 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011008{
11009 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011010 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011011
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011012 /*
11013 * First, the fields that are shadowed. This must be kept in sync
11014 * with vmx_shadow_fields.h.
11015 */
11016
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011017 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011018 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011019 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011020 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11021 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011022
11023 /*
11024 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11025 * HOST_FS_BASE, HOST_GS_BASE.
11026 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011027
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011028 if (from_vmentry &&
11029 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011030 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11031 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11032 } else {
11033 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11034 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11035 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011036 if (from_vmentry) {
11037 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11038 vmcs12->vm_entry_intr_info_field);
11039 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11040 vmcs12->vm_entry_exception_error_code);
11041 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11042 vmcs12->vm_entry_instruction_len);
11043 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11044 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011045 vmx->loaded_vmcs->nmi_known_unmasked =
11046 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011047 } else {
11048 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11049 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011050 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011051
Jan Kiszkaf41245002014-03-07 20:03:13 +010011052 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011053
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011054 /* Preemption timer setting is only taken from vmcs01. */
11055 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11056 exec_control |= vmcs_config.pin_based_exec_ctrl;
11057 if (vmx->hv_deadline_tsc == -1)
11058 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11059
11060 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011061 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011062 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11063 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011064 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011065 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011066 }
Wincy Van705699a2015-02-03 23:58:17 +080011067
Jan Kiszkaf41245002014-03-07 20:03:13 +010011068 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011069
Jan Kiszkaf41245002014-03-07 20:03:13 +010011070 vmx->nested.preemption_timer_expired = false;
11071 if (nested_cpu_has_preemption_timer(vmcs12))
11072 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011073
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011074 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011075 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011076
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011077 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011078 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011079 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011080 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011081 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011082 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011083 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11084 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011085 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011086 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11087 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11088 ~SECONDARY_EXEC_ENABLE_PML;
11089 exec_control |= vmcs12_exec_ctrl;
11090 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011091
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011092 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011093 vmcs_write16(GUEST_INTR_STATUS,
11094 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011095
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011096 /*
11097 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11098 * nested_get_vmcs12_pages will either fix it up or
11099 * remove the VM execution control.
11100 */
11101 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11102 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11103
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011104 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11105 }
11106
Jim Mattson83bafef2016-10-04 10:48:38 -070011107 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011108 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11109 * entry, but only if the current (host) sp changed from the value
11110 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11111 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11112 * here we just force the write to happen on entry.
11113 */
11114 vmx->host_rsp = 0;
11115
11116 exec_control = vmx_exec_control(vmx); /* L0's desires */
11117 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11118 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11119 exec_control &= ~CPU_BASED_TPR_SHADOW;
11120 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011121
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011122 /*
11123 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11124 * nested_get_vmcs12_pages can't fix it up, the illegal value
11125 * will result in a VM entry failure.
11126 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011127 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011128 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011129 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011130 } else {
11131#ifdef CONFIG_X86_64
11132 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11133 CPU_BASED_CR8_STORE_EXITING;
11134#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011135 }
11136
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011137 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011138 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11139 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011140 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011141 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11142 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11143
11144 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11145
11146 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11147 * bitwise-or of what L1 wants to trap for L2, and what we want to
11148 * trap. Note that CR0.TS also needs updating - we do this later.
11149 */
11150 update_exception_bitmap(vcpu);
11151 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11152 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11153
Nadav Har'El8049d652013-08-05 11:07:06 +030011154 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11155 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11156 * bits are further modified by vmx_set_efer() below.
11157 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011158 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011159
11160 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11161 * emulated by vmx_set_efer(), below.
11162 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011163 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011164 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11165 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011166 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11167
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011168 if (from_vmentry &&
11169 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011170 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011171 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011172 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011173 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011174 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011175
Nadav Har'El27fc51b2011-08-02 15:54:52 +030011176 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11177 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011178 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030011179 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011180 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070011181 if (kvm_has_tsc_control)
11182 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011183
11184 if (enable_vpid) {
11185 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011186 * There is no direct mapping between vpid02 and vpid12, the
11187 * vpid02 is per-vCPU for L0 and reused while the value of
11188 * vpid12 is changed w/ one invvpid during nested vmentry.
11189 * The vpid12 is allocated by L1 for L2, so it will not
11190 * influence global bitmap(for vpid01 and vpid02 allocation)
11191 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011192 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011193 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011194 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11195 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011196 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011197 }
11198 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011199 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011200 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011201 }
11202
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011203 if (enable_pml) {
11204 /*
11205 * Conceptually we want to copy the PML address and index from
11206 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11207 * since we always flush the log on each vmexit, this happens
11208 * to be equivalent to simply resetting the fields in vmcs02.
11209 */
11210 ASSERT(vmx->pml_pg);
11211 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11212 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11213 }
11214
Nadav Har'El155a97a2013-08-05 11:07:16 +030011215 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011216 if (nested_ept_init_mmu_context(vcpu)) {
11217 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11218 return 1;
11219 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011220 } else if (nested_cpu_has2(vmcs12,
11221 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11222 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011223 }
11224
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011225 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011226 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11227 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011228 * The CR0_READ_SHADOW is what L2 should have expected to read given
11229 * the specifications by L1; It's not enough to take
11230 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11231 * have more bits than L1 expected.
11232 */
11233 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11234 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11235
11236 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11237 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11238
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011239 if (from_vmentry &&
11240 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011241 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11242 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11243 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11244 else
11245 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11246 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11247 vmx_set_efer(vcpu, vcpu->arch.efer);
11248
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011249 if (vmx->nested.dirty_vmcs12) {
11250 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
11251 vmx->nested.dirty_vmcs12 = false;
11252 }
11253
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011254 /*
11255 * Guest state is invalid and unrestricted guest is disabled,
11256 * which means L1 attempted VMEntry to L2 with invalid state.
11257 * Fail the VMEntry.
11258 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011259 if (vmx->emulation_required) {
11260 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011261 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011262 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011263
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011264 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011265 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011266 entry_failure_code))
11267 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011268
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011269 if (!enable_ept)
11270 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11271
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011272 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11273 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011274 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011275}
11276
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011277static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11278{
11279 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11280 nested_cpu_has_virtual_nmis(vmcs12))
11281 return -EINVAL;
11282
11283 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11284 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11285 return -EINVAL;
11286
11287 return 0;
11288}
11289
Jim Mattsonca0bde22016-11-30 12:03:46 -080011290static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11291{
11292 struct vcpu_vmx *vmx = to_vmx(vcpu);
11293
11294 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11295 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11296 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11297
Jim Mattson56a20512017-07-06 16:33:06 -070011298 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11299 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11300
Jim Mattsonca0bde22016-11-30 12:03:46 -080011301 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11302 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11303
Jim Mattson712b12d2017-08-24 13:24:47 -070011304 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11305 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11306
Jim Mattsonca0bde22016-11-30 12:03:46 -080011307 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11308 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11309
11310 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11311 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11312
Bandan Dasc5f983f2017-05-05 15:25:14 -040011313 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11314 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11315
Jim Mattsonca0bde22016-11-30 12:03:46 -080011316 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011317 vmx->nested.msrs.procbased_ctls_low,
11318 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011319 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11320 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011321 vmx->nested.msrs.secondary_ctls_low,
11322 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011323 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011324 vmx->nested.msrs.pinbased_ctls_low,
11325 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011326 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011327 vmx->nested.msrs.exit_ctls_low,
11328 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011329 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011330 vmx->nested.msrs.entry_ctls_low,
11331 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011332 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11333
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011334 if (nested_vmx_check_nmi_controls(vmcs12))
11335 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11336
Bandan Das41ab9372017-08-03 15:54:43 -040011337 if (nested_cpu_has_vmfunc(vmcs12)) {
11338 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011339 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011340 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11341
11342 if (nested_cpu_has_eptp_switching(vmcs12)) {
11343 if (!nested_cpu_has_ept(vmcs12) ||
11344 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11345 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11346 }
11347 }
Bandan Das27c42a12017-08-03 15:54:42 -040011348
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011349 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11350 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11351
Jim Mattsonca0bde22016-11-30 12:03:46 -080011352 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11353 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11354 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11355 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11356
11357 return 0;
11358}
11359
11360static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11361 u32 *exit_qual)
11362{
11363 bool ia32e;
11364
11365 *exit_qual = ENTRY_FAIL_DEFAULT;
11366
11367 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11368 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11369 return 1;
11370
11371 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11372 vmcs12->vmcs_link_pointer != -1ull) {
11373 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11374 return 1;
11375 }
11376
11377 /*
11378 * If the load IA32_EFER VM-entry control is 1, the following checks
11379 * are performed on the field for the IA32_EFER MSR:
11380 * - Bits reserved in the IA32_EFER MSR must be 0.
11381 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11382 * the IA-32e mode guest VM-exit control. It must also be identical
11383 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11384 * CR0.PG) is 1.
11385 */
11386 if (to_vmx(vcpu)->nested.nested_run_pending &&
11387 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11388 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11389 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11390 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11391 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11392 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11393 return 1;
11394 }
11395
11396 /*
11397 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11398 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11399 * the values of the LMA and LME bits in the field must each be that of
11400 * the host address-space size VM-exit control.
11401 */
11402 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11403 ia32e = (vmcs12->vm_exit_controls &
11404 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11405 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11406 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11407 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11408 return 1;
11409 }
11410
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011411 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11412 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11413 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11414 return 1;
11415
Jim Mattsonca0bde22016-11-30 12:03:46 -080011416 return 0;
11417}
11418
Jim Mattson858e25c2016-11-30 12:03:47 -080011419static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11420{
11421 struct vcpu_vmx *vmx = to_vmx(vcpu);
11422 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011423 u32 msr_entry_idx;
11424 u32 exit_qual;
11425
Jim Mattson858e25c2016-11-30 12:03:47 -080011426 enter_guest_mode(vcpu);
11427
11428 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11429 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11430
Jim Mattsonde3a0022017-11-27 17:22:25 -060011431 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011432 vmx_segment_cache_clear(vmx);
11433
11434 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11435 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011436 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011437 nested_vmx_entry_failure(vcpu, vmcs12,
11438 EXIT_REASON_INVALID_STATE, exit_qual);
11439 return 1;
11440 }
11441
11442 nested_get_vmcs12_pages(vcpu, vmcs12);
11443
11444 msr_entry_idx = nested_vmx_load_msr(vcpu,
11445 vmcs12->vm_entry_msr_load_addr,
11446 vmcs12->vm_entry_msr_load_count);
11447 if (msr_entry_idx) {
11448 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011449 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011450 nested_vmx_entry_failure(vcpu, vmcs12,
11451 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11452 return 1;
11453 }
11454
Jim Mattson858e25c2016-11-30 12:03:47 -080011455 /*
11456 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11457 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11458 * returned as far as L1 is concerned. It will only return (and set
11459 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11460 */
11461 return 0;
11462}
11463
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011464/*
11465 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11466 * for running an L2 nested guest.
11467 */
11468static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11469{
11470 struct vmcs12 *vmcs12;
11471 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011472 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011473 u32 exit_qual;
11474 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011475
Kyle Hueyeb277562016-11-29 12:40:39 -080011476 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011477 return 1;
11478
Kyle Hueyeb277562016-11-29 12:40:39 -080011479 if (!nested_vmx_check_vmcs12(vcpu))
11480 goto out;
11481
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011482 vmcs12 = get_vmcs12(vcpu);
11483
Abel Gordon012f83c2013-04-18 14:39:25 +030011484 if (enable_shadow_vmcs)
11485 copy_shadow_to_vmcs12(vmx);
11486
Nadav Har'El7c177932011-05-25 23:12:04 +030011487 /*
11488 * The nested entry process starts with enforcing various prerequisites
11489 * on vmcs12 as required by the Intel SDM, and act appropriately when
11490 * they fail: As the SDM explains, some conditions should cause the
11491 * instruction to fail, while others will cause the instruction to seem
11492 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11493 * To speed up the normal (success) code path, we should avoid checking
11494 * for misconfigurations which will anyway be caught by the processor
11495 * when using the merged vmcs02.
11496 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011497 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11498 nested_vmx_failValid(vcpu,
11499 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11500 goto out;
11501 }
11502
Nadav Har'El7c177932011-05-25 23:12:04 +030011503 if (vmcs12->launch_state == launch) {
11504 nested_vmx_failValid(vcpu,
11505 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11506 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011507 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011508 }
11509
Jim Mattsonca0bde22016-11-30 12:03:46 -080011510 ret = check_vmentry_prereqs(vcpu, vmcs12);
11511 if (ret) {
11512 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011513 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011514 }
11515
Nadav Har'El7c177932011-05-25 23:12:04 +030011516 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011517 * After this point, the trap flag no longer triggers a singlestep trap
11518 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11519 * This is not 100% correct; for performance reasons, we delegate most
11520 * of the checks on host state to the processor. If those fail,
11521 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011522 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011523 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011524
Jim Mattsonca0bde22016-11-30 12:03:46 -080011525 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11526 if (ret) {
11527 nested_vmx_entry_failure(vcpu, vmcs12,
11528 EXIT_REASON_INVALID_STATE, exit_qual);
11529 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011530 }
11531
11532 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011533 * We're finally done with prerequisite checking, and can start with
11534 * the nested entry.
11535 */
11536
Jim Mattson858e25c2016-11-30 12:03:47 -080011537 ret = enter_vmx_non_root_mode(vcpu, true);
11538 if (ret)
11539 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011540
Chao Gao135a06c2018-02-11 10:06:30 +080011541 /*
11542 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11543 * by event injection, halt vcpu.
11544 */
11545 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11546 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
Joel Schopp5cb56052015-03-02 13:43:31 -060011547 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011548
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011549 vmx->nested.nested_run_pending = 1;
11550
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011551 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011552
11553out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011554 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011555}
11556
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011557/*
11558 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11559 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11560 * This function returns the new value we should put in vmcs12.guest_cr0.
11561 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11562 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11563 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11564 * didn't trap the bit, because if L1 did, so would L0).
11565 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11566 * been modified by L2, and L1 knows it. So just leave the old value of
11567 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11568 * isn't relevant, because if L0 traps this bit it can set it to anything.
11569 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11570 * changed these bits, and therefore they need to be updated, but L0
11571 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11572 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11573 */
11574static inline unsigned long
11575vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11576{
11577 return
11578 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11579 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11580 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11581 vcpu->arch.cr0_guest_owned_bits));
11582}
11583
11584static inline unsigned long
11585vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11586{
11587 return
11588 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11589 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11590 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11591 vcpu->arch.cr4_guest_owned_bits));
11592}
11593
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011594static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11595 struct vmcs12 *vmcs12)
11596{
11597 u32 idt_vectoring;
11598 unsigned int nr;
11599
Wanpeng Li664f8e22017-08-24 03:35:09 -070011600 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011601 nr = vcpu->arch.exception.nr;
11602 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11603
11604 if (kvm_exception_is_soft(nr)) {
11605 vmcs12->vm_exit_instruction_len =
11606 vcpu->arch.event_exit_inst_len;
11607 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11608 } else
11609 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11610
11611 if (vcpu->arch.exception.has_error_code) {
11612 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11613 vmcs12->idt_vectoring_error_code =
11614 vcpu->arch.exception.error_code;
11615 }
11616
11617 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011618 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011619 vmcs12->idt_vectoring_info_field =
11620 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011621 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011622 nr = vcpu->arch.interrupt.nr;
11623 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11624
11625 if (vcpu->arch.interrupt.soft) {
11626 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11627 vmcs12->vm_entry_instruction_len =
11628 vcpu->arch.event_exit_inst_len;
11629 } else
11630 idt_vectoring |= INTR_TYPE_EXT_INTR;
11631
11632 vmcs12->idt_vectoring_info_field = idt_vectoring;
11633 }
11634}
11635
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011636static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11637{
11638 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011639 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011640 bool block_nested_events =
11641 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011642
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011643 if (vcpu->arch.exception.pending &&
11644 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011645 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011646 return -EBUSY;
11647 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011648 return 0;
11649 }
11650
Jan Kiszkaf41245002014-03-07 20:03:13 +010011651 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11652 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011653 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011654 return -EBUSY;
11655 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11656 return 0;
11657 }
11658
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011659 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011660 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011661 return -EBUSY;
11662 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11663 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11664 INTR_INFO_VALID_MASK, 0);
11665 /*
11666 * The NMI-triggered VM exit counts as injection:
11667 * clear this one and block further NMIs.
11668 */
11669 vcpu->arch.nmi_pending = 0;
11670 vmx_set_nmi_mask(vcpu, true);
11671 return 0;
11672 }
11673
11674 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11675 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011676 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011677 return -EBUSY;
11678 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011679 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011680 }
11681
David Hildenbrand6342c502017-01-25 11:58:58 +010011682 vmx_complete_nested_posted_interrupt(vcpu);
11683 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011684}
11685
Jan Kiszkaf41245002014-03-07 20:03:13 +010011686static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11687{
11688 ktime_t remaining =
11689 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11690 u64 value;
11691
11692 if (ktime_to_ns(remaining) <= 0)
11693 return 0;
11694
11695 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11696 do_div(value, 1000000);
11697 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11698}
11699
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011700/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011701 * Update the guest state fields of vmcs12 to reflect changes that
11702 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11703 * VM-entry controls is also updated, since this is really a guest
11704 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011705 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011706static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011707{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011708 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11709 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11710
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011711 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11712 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11713 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11714
11715 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11716 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11717 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11718 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11719 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11720 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11721 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11722 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11723 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11724 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11725 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11726 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11727 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11728 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11729 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11730 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11731 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11732 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11733 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11734 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11735 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11736 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11737 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11738 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11739 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11740 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11741 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11742 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11743 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11744 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11745 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11746 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11747 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11748 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11749 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11750 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11751
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011752 vmcs12->guest_interruptibility_info =
11753 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11754 vmcs12->guest_pending_dbg_exceptions =
11755 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011756 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11757 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11758 else
11759 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011760
Jan Kiszkaf41245002014-03-07 20:03:13 +010011761 if (nested_cpu_has_preemption_timer(vmcs12)) {
11762 if (vmcs12->vm_exit_controls &
11763 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11764 vmcs12->vmx_preemption_timer_value =
11765 vmx_get_preemption_timer_value(vcpu);
11766 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11767 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011768
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011769 /*
11770 * In some cases (usually, nested EPT), L2 is allowed to change its
11771 * own CR3 without exiting. If it has changed it, we must keep it.
11772 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11773 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11774 *
11775 * Additionally, restore L2's PDPTR to vmcs12.
11776 */
11777 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011778 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011779 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11780 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11781 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11782 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11783 }
11784
Jim Mattsond281e132017-06-01 12:44:46 -070011785 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011786
Wincy Van608406e2015-02-03 23:57:51 +080011787 if (nested_cpu_has_vid(vmcs12))
11788 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11789
Jan Kiszkac18911a2013-03-13 16:06:41 +010011790 vmcs12->vm_entry_controls =
11791 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011792 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011793
Jan Kiszka2996fca2014-06-16 13:59:43 +020011794 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11795 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11796 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11797 }
11798
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011799 /* TODO: These cannot have changed unless we have MSR bitmaps and
11800 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011801 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011802 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011803 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11804 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011805 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11806 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11807 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011808 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011809 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011810}
11811
11812/*
11813 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11814 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11815 * and this function updates it to reflect the changes to the guest state while
11816 * L2 was running (and perhaps made some exits which were handled directly by L0
11817 * without going back to L1), and to reflect the exit reason.
11818 * Note that we do not have to copy here all VMCS fields, just those that
11819 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11820 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11821 * which already writes to vmcs12 directly.
11822 */
11823static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11824 u32 exit_reason, u32 exit_intr_info,
11825 unsigned long exit_qualification)
11826{
11827 /* update guest state fields: */
11828 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011829
11830 /* update exit information fields: */
11831
Jan Kiszka533558b2014-01-04 18:47:20 +010011832 vmcs12->vm_exit_reason = exit_reason;
11833 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011834 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011835
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011836 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011837 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11838 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11839
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011840 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011841 vmcs12->launch_state = 1;
11842
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011843 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11844 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011845 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011846
11847 /*
11848 * Transfer the event that L0 or L1 may wanted to inject into
11849 * L2 to IDT_VECTORING_INFO_FIELD.
11850 */
11851 vmcs12_save_pending_event(vcpu, vmcs12);
11852 }
11853
11854 /*
11855 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11856 * preserved above and would only end up incorrectly in L1.
11857 */
11858 vcpu->arch.nmi_injected = false;
11859 kvm_clear_exception_queue(vcpu);
11860 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011861}
11862
Wanpeng Li5af41572017-11-05 16:54:49 -080011863static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11864 struct vmcs12 *vmcs12)
11865{
11866 u32 entry_failure_code;
11867
11868 nested_ept_uninit_mmu_context(vcpu);
11869
11870 /*
11871 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11872 * couldn't have changed.
11873 */
11874 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11875 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11876
11877 if (!enable_ept)
11878 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11879}
11880
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011881/*
11882 * A part of what we need to when the nested L2 guest exits and we want to
11883 * run its L1 parent, is to reset L1's guest state to the host state specified
11884 * in vmcs12.
11885 * This function is to be called not only on normal nested exit, but also on
11886 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11887 * Failures During or After Loading Guest State").
11888 * This function should be called when the active VMCS is L1's (vmcs01).
11889 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011890static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11891 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011892{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011893 struct kvm_segment seg;
11894
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011895 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11896 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011897 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011898 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11899 else
11900 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11901 vmx_set_efer(vcpu, vcpu->arch.efer);
11902
11903 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11904 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011905 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011906 /*
11907 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011908 * actually changed, because vmx_set_cr0 refers to efer set above.
11909 *
11910 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11911 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011912 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011913 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020011914 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011915
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011916 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011917 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011918 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011919
Wanpeng Li5af41572017-11-05 16:54:49 -080011920 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011921
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011922 if (enable_vpid) {
11923 /*
11924 * Trivially support vpid by letting L2s share their parent
11925 * L1's vpid. TODO: move to a more elaborate solution, giving
11926 * each L2 its own vpid and exposing the vpid feature to L1.
11927 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011928 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011929 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011930
11931 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11932 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11933 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11934 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11935 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011936 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11937 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011938
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011939 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11940 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11941 vmcs_write64(GUEST_BNDCFGS, 0);
11942
Jan Kiszka44811c02013-08-04 17:17:27 +020011943 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011944 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011945 vcpu->arch.pat = vmcs12->host_ia32_pat;
11946 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011947 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11948 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11949 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011950
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011951 /* Set L1 segment info according to Intel SDM
11952 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11953 seg = (struct kvm_segment) {
11954 .base = 0,
11955 .limit = 0xFFFFFFFF,
11956 .selector = vmcs12->host_cs_selector,
11957 .type = 11,
11958 .present = 1,
11959 .s = 1,
11960 .g = 1
11961 };
11962 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11963 seg.l = 1;
11964 else
11965 seg.db = 1;
11966 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11967 seg = (struct kvm_segment) {
11968 .base = 0,
11969 .limit = 0xFFFFFFFF,
11970 .type = 3,
11971 .present = 1,
11972 .s = 1,
11973 .db = 1,
11974 .g = 1
11975 };
11976 seg.selector = vmcs12->host_ds_selector;
11977 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11978 seg.selector = vmcs12->host_es_selector;
11979 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11980 seg.selector = vmcs12->host_ss_selector;
11981 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11982 seg.selector = vmcs12->host_fs_selector;
11983 seg.base = vmcs12->host_fs_base;
11984 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11985 seg.selector = vmcs12->host_gs_selector;
11986 seg.base = vmcs12->host_gs_base;
11987 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11988 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011989 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011990 .limit = 0x67,
11991 .selector = vmcs12->host_tr_selector,
11992 .type = 11,
11993 .present = 1
11994 };
11995 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11996
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011997 kvm_set_dr(vcpu, 7, 0x400);
11998 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011999
Wincy Van3af18d92015-02-03 23:49:31 +080012000 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012001 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012002
Wincy Vanff651cb2014-12-11 08:52:58 +030012003 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12004 vmcs12->vm_exit_msr_load_count))
12005 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012006}
12007
12008/*
12009 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12010 * and modify vmcs12 to make it see what it would expect to see there if
12011 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12012 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012013static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12014 u32 exit_intr_info,
12015 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012016{
12017 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012018 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12019
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012020 /* trying to cancel vmlaunch/vmresume is a bug */
12021 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12022
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012023 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012024 * The only expected VM-instruction error is "VM entry with
12025 * invalid control field(s)." Anything else indicates a
12026 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012027 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012028 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12029 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12030
12031 leave_guest_mode(vcpu);
12032
12033 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012034 if (exit_reason == -1)
12035 sync_vmcs12(vcpu, vmcs12);
12036 else
12037 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12038 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012039
12040 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12041 vmcs12->vm_exit_msr_store_count))
12042 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012043 }
12044
Jim Mattson4f350c62017-09-14 16:31:44 -070012045 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012046 vm_entry_controls_reset_shadow(vmx);
12047 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012048 vmx_segment_cache_clear(vmx);
12049
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012050 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012051 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12052 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012053 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012054 if (vmx->hv_deadline_tsc == -1)
12055 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12056 PIN_BASED_VMX_PREEMPTION_TIMER);
12057 else
12058 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12059 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012060 if (kvm_has_tsc_control)
12061 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012062
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012063 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
12064 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
12065 vmx_set_virtual_x2apic_mode(vcpu,
12066 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012067 } else if (!nested_cpu_has_ept(vmcs12) &&
12068 nested_cpu_has2(vmcs12,
12069 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
12070 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012071 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012072
12073 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12074 vmx->host_rsp = 0;
12075
12076 /* Unpin physical memory we referred to in vmcs02 */
12077 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012078 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012079 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012080 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012081 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012082 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012083 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012084 }
Wincy Van705699a2015-02-03 23:58:17 +080012085 if (vmx->nested.pi_desc_page) {
12086 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012087 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012088 vmx->nested.pi_desc_page = NULL;
12089 vmx->nested.pi_desc = NULL;
12090 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012091
12092 /*
Tang Chen38b99172014-09-24 15:57:54 +080012093 * We are now running in L2, mmu_notifier will force to reload the
12094 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12095 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012096 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012097
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012098 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012099 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012100
12101 /* in case we halted in L2 */
12102 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012103
12104 if (likely(!vmx->fail)) {
12105 /*
12106 * TODO: SDM says that with acknowledge interrupt on
12107 * exit, bit 31 of the VM-exit interrupt information
12108 * (valid interrupt) is always set to 1 on
12109 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12110 * need kvm_cpu_has_interrupt(). See the commit
12111 * message for details.
12112 */
12113 if (nested_exit_intr_ack_set(vcpu) &&
12114 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12115 kvm_cpu_has_interrupt(vcpu)) {
12116 int irq = kvm_cpu_get_interrupt(vcpu);
12117 WARN_ON(irq < 0);
12118 vmcs12->vm_exit_intr_info = irq |
12119 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12120 }
12121
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012122 if (exit_reason != -1)
12123 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12124 vmcs12->exit_qualification,
12125 vmcs12->idt_vectoring_info_field,
12126 vmcs12->vm_exit_intr_info,
12127 vmcs12->vm_exit_intr_error_code,
12128 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012129
12130 load_vmcs12_host_state(vcpu, vmcs12);
12131
12132 return;
12133 }
12134
12135 /*
12136 * After an early L2 VM-entry failure, we're now back
12137 * in L1 which thinks it just finished a VMLAUNCH or
12138 * VMRESUME instruction, so we need to set the failure
12139 * flag and the VM-instruction error field of the VMCS
12140 * accordingly.
12141 */
12142 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012143
12144 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12145
Jim Mattson4f350c62017-09-14 16:31:44 -070012146 /*
12147 * The emulated instruction was already skipped in
12148 * nested_vmx_run, but the updated RIP was never
12149 * written back to the vmcs01.
12150 */
12151 skip_emulated_instruction(vcpu);
12152 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012153}
12154
Nadav Har'El7c177932011-05-25 23:12:04 +030012155/*
Jan Kiszka42124922014-01-04 18:47:19 +010012156 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12157 */
12158static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12159{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012160 if (is_guest_mode(vcpu)) {
12161 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012162 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012163 }
Jan Kiszka42124922014-01-04 18:47:19 +010012164 free_nested(to_vmx(vcpu));
12165}
12166
12167/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012168 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12169 * 23.7 "VM-entry failures during or after loading guest state" (this also
12170 * lists the acceptable exit-reason and exit-qualification parameters).
12171 * It should only be called before L2 actually succeeded to run, and when
12172 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12173 */
12174static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12175 struct vmcs12 *vmcs12,
12176 u32 reason, unsigned long qualification)
12177{
12178 load_vmcs12_host_state(vcpu, vmcs12);
12179 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12180 vmcs12->exit_qualification = qualification;
12181 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012182 if (enable_shadow_vmcs)
12183 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012184}
12185
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012186static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12187 struct x86_instruction_info *info,
12188 enum x86_intercept_stage stage)
12189{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012190 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12191 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12192
12193 /*
12194 * RDPID causes #UD if disabled through secondary execution controls.
12195 * Because it is marked as EmulateOnUD, we need to intercept it here.
12196 */
12197 if (info->intercept == x86_intercept_rdtscp &&
12198 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12199 ctxt->exception.vector = UD_VECTOR;
12200 ctxt->exception.error_code_valid = false;
12201 return X86EMUL_PROPAGATE_FAULT;
12202 }
12203
12204 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012205 return X86EMUL_CONTINUE;
12206}
12207
Yunhong Jiang64672c92016-06-13 14:19:59 -070012208#ifdef CONFIG_X86_64
12209/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12210static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12211 u64 divisor, u64 *result)
12212{
12213 u64 low = a << shift, high = a >> (64 - shift);
12214
12215 /* To avoid the overflow on divq */
12216 if (high >= divisor)
12217 return 1;
12218
12219 /* Low hold the result, high hold rem which is discarded */
12220 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12221 "rm" (divisor), "0" (low), "1" (high));
12222 *result = low;
12223
12224 return 0;
12225}
12226
12227static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12228{
12229 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020012230 u64 tscl = rdtsc();
12231 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12232 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012233
12234 /* Convert to host delta tsc if tsc scaling is enabled */
12235 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12236 u64_shl_div_u64(delta_tsc,
12237 kvm_tsc_scaling_ratio_frac_bits,
12238 vcpu->arch.tsc_scaling_ratio,
12239 &delta_tsc))
12240 return -ERANGE;
12241
12242 /*
12243 * If the delta tsc can't fit in the 32 bit after the multi shift,
12244 * we can't use the preemption timer.
12245 * It's possible that it fits on later vmentries, but checking
12246 * on every vmentry is costly so we just use an hrtimer.
12247 */
12248 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12249 return -ERANGE;
12250
12251 vmx->hv_deadline_tsc = tscl + delta_tsc;
12252 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12253 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012254
12255 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012256}
12257
12258static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12259{
12260 struct vcpu_vmx *vmx = to_vmx(vcpu);
12261 vmx->hv_deadline_tsc = -1;
12262 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12263 PIN_BASED_VMX_PREEMPTION_TIMER);
12264}
12265#endif
12266
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012267static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012268{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012269 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012270 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012271}
12272
Kai Huang843e4332015-01-28 10:54:28 +080012273static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12274 struct kvm_memory_slot *slot)
12275{
12276 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12277 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12278}
12279
12280static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12281 struct kvm_memory_slot *slot)
12282{
12283 kvm_mmu_slot_set_dirty(kvm, slot);
12284}
12285
12286static void vmx_flush_log_dirty(struct kvm *kvm)
12287{
12288 kvm_flush_pml_buffers(kvm);
12289}
12290
Bandan Dasc5f983f2017-05-05 15:25:14 -040012291static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12292{
12293 struct vmcs12 *vmcs12;
12294 struct vcpu_vmx *vmx = to_vmx(vcpu);
12295 gpa_t gpa;
12296 struct page *page = NULL;
12297 u64 *pml_address;
12298
12299 if (is_guest_mode(vcpu)) {
12300 WARN_ON_ONCE(vmx->nested.pml_full);
12301
12302 /*
12303 * Check if PML is enabled for the nested guest.
12304 * Whether eptp bit 6 is set is already checked
12305 * as part of A/D emulation.
12306 */
12307 vmcs12 = get_vmcs12(vcpu);
12308 if (!nested_cpu_has_pml(vmcs12))
12309 return 0;
12310
Dan Carpenter47698862017-05-10 22:43:17 +030012311 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012312 vmx->nested.pml_full = true;
12313 return 1;
12314 }
12315
12316 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12317
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012318 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12319 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012320 return 0;
12321
12322 pml_address = kmap(page);
12323 pml_address[vmcs12->guest_pml_index--] = gpa;
12324 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012325 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012326 }
12327
12328 return 0;
12329}
12330
Kai Huang843e4332015-01-28 10:54:28 +080012331static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12332 struct kvm_memory_slot *memslot,
12333 gfn_t offset, unsigned long mask)
12334{
12335 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12336}
12337
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012338static void __pi_post_block(struct kvm_vcpu *vcpu)
12339{
12340 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12341 struct pi_desc old, new;
12342 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012343
12344 do {
12345 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012346 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12347 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012348
12349 dest = cpu_physical_id(vcpu->cpu);
12350
12351 if (x2apic_enabled())
12352 new.ndst = dest;
12353 else
12354 new.ndst = (dest << 8) & 0xFF00;
12355
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012356 /* set 'NV' to 'notification vector' */
12357 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012358 } while (cmpxchg64(&pi_desc->control, old.control,
12359 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012360
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012361 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12362 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012363 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012364 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012365 vcpu->pre_pcpu = -1;
12366 }
12367}
12368
Feng Wuefc64402015-09-18 22:29:51 +080012369/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012370 * This routine does the following things for vCPU which is going
12371 * to be blocked if VT-d PI is enabled.
12372 * - Store the vCPU to the wakeup list, so when interrupts happen
12373 * we can find the right vCPU to wake up.
12374 * - Change the Posted-interrupt descriptor as below:
12375 * 'NDST' <-- vcpu->pre_pcpu
12376 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12377 * - If 'ON' is set during this process, which means at least one
12378 * interrupt is posted for this vCPU, we cannot block it, in
12379 * this case, return 1, otherwise, return 0.
12380 *
12381 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012382static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012383{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012384 unsigned int dest;
12385 struct pi_desc old, new;
12386 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12387
12388 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012389 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12390 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012391 return 0;
12392
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012393 WARN_ON(irqs_disabled());
12394 local_irq_disable();
12395 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12396 vcpu->pre_pcpu = vcpu->cpu;
12397 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12398 list_add_tail(&vcpu->blocked_vcpu_list,
12399 &per_cpu(blocked_vcpu_on_cpu,
12400 vcpu->pre_pcpu));
12401 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12402 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012403
12404 do {
12405 old.control = new.control = pi_desc->control;
12406
Feng Wubf9f6ac2015-09-18 22:29:55 +080012407 WARN((pi_desc->sn == 1),
12408 "Warning: SN field of posted-interrupts "
12409 "is set before blocking\n");
12410
12411 /*
12412 * Since vCPU can be preempted during this process,
12413 * vcpu->cpu could be different with pre_pcpu, we
12414 * need to set pre_pcpu as the destination of wakeup
12415 * notification event, then we can find the right vCPU
12416 * to wakeup in wakeup handler if interrupts happen
12417 * when the vCPU is in blocked state.
12418 */
12419 dest = cpu_physical_id(vcpu->pre_pcpu);
12420
12421 if (x2apic_enabled())
12422 new.ndst = dest;
12423 else
12424 new.ndst = (dest << 8) & 0xFF00;
12425
12426 /* set 'NV' to 'wakeup vector' */
12427 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012428 } while (cmpxchg64(&pi_desc->control, old.control,
12429 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012430
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012431 /* We should not block the vCPU if an interrupt is posted for it. */
12432 if (pi_test_on(pi_desc) == 1)
12433 __pi_post_block(vcpu);
12434
12435 local_irq_enable();
12436 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012437}
12438
Yunhong Jiangbc225122016-06-13 14:19:58 -070012439static int vmx_pre_block(struct kvm_vcpu *vcpu)
12440{
12441 if (pi_pre_block(vcpu))
12442 return 1;
12443
Yunhong Jiang64672c92016-06-13 14:19:59 -070012444 if (kvm_lapic_hv_timer_in_use(vcpu))
12445 kvm_lapic_switch_to_sw_timer(vcpu);
12446
Yunhong Jiangbc225122016-06-13 14:19:58 -070012447 return 0;
12448}
12449
12450static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012451{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012452 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012453 return;
12454
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012455 WARN_ON(irqs_disabled());
12456 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012457 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012458 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012459}
12460
Yunhong Jiangbc225122016-06-13 14:19:58 -070012461static void vmx_post_block(struct kvm_vcpu *vcpu)
12462{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012463 if (kvm_x86_ops->set_hv_timer)
12464 kvm_lapic_switch_to_hv_timer(vcpu);
12465
Yunhong Jiangbc225122016-06-13 14:19:58 -070012466 pi_post_block(vcpu);
12467}
12468
Feng Wubf9f6ac2015-09-18 22:29:55 +080012469/*
Feng Wuefc64402015-09-18 22:29:51 +080012470 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12471 *
12472 * @kvm: kvm
12473 * @host_irq: host irq of the interrupt
12474 * @guest_irq: gsi of the interrupt
12475 * @set: set or unset PI
12476 * returns 0 on success, < 0 on failure
12477 */
12478static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12479 uint32_t guest_irq, bool set)
12480{
12481 struct kvm_kernel_irq_routing_entry *e;
12482 struct kvm_irq_routing_table *irq_rt;
12483 struct kvm_lapic_irq irq;
12484 struct kvm_vcpu *vcpu;
12485 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012486 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012487
12488 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012489 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12490 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012491 return 0;
12492
12493 idx = srcu_read_lock(&kvm->irq_srcu);
12494 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012495 if (guest_irq >= irq_rt->nr_rt_entries ||
12496 hlist_empty(&irq_rt->map[guest_irq])) {
12497 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12498 guest_irq, irq_rt->nr_rt_entries);
12499 goto out;
12500 }
Feng Wuefc64402015-09-18 22:29:51 +080012501
12502 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12503 if (e->type != KVM_IRQ_ROUTING_MSI)
12504 continue;
12505 /*
12506 * VT-d PI cannot support posting multicast/broadcast
12507 * interrupts to a vCPU, we still use interrupt remapping
12508 * for these kind of interrupts.
12509 *
12510 * For lowest-priority interrupts, we only support
12511 * those with single CPU as the destination, e.g. user
12512 * configures the interrupts via /proc/irq or uses
12513 * irqbalance to make the interrupts single-CPU.
12514 *
12515 * We will support full lowest-priority interrupt later.
12516 */
12517
Radim Krčmář371313132016-07-12 22:09:27 +020012518 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012519 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12520 /*
12521 * Make sure the IRTE is in remapped mode if
12522 * we don't handle it in posted mode.
12523 */
12524 ret = irq_set_vcpu_affinity(host_irq, NULL);
12525 if (ret < 0) {
12526 printk(KERN_INFO
12527 "failed to back to remapped mode, irq: %u\n",
12528 host_irq);
12529 goto out;
12530 }
12531
Feng Wuefc64402015-09-18 22:29:51 +080012532 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012533 }
Feng Wuefc64402015-09-18 22:29:51 +080012534
12535 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12536 vcpu_info.vector = irq.vector;
12537
Feng Wub6ce9782016-01-25 16:53:35 +080012538 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012539 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12540
12541 if (set)
12542 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012543 else
Feng Wuefc64402015-09-18 22:29:51 +080012544 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012545
12546 if (ret < 0) {
12547 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12548 __func__);
12549 goto out;
12550 }
12551 }
12552
12553 ret = 0;
12554out:
12555 srcu_read_unlock(&kvm->irq_srcu, idx);
12556 return ret;
12557}
12558
Ashok Rajc45dcc72016-06-22 14:59:56 +080012559static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12560{
12561 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12562 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12563 FEATURE_CONTROL_LMCE;
12564 else
12565 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12566 ~FEATURE_CONTROL_LMCE;
12567}
12568
Ladi Prosek72d7b372017-10-11 16:54:41 +020012569static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12570{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012571 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12572 if (to_vmx(vcpu)->nested.nested_run_pending)
12573 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012574 return 1;
12575}
12576
Ladi Prosek0234bf82017-10-11 16:54:40 +020012577static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12578{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012579 struct vcpu_vmx *vmx = to_vmx(vcpu);
12580
12581 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12582 if (vmx->nested.smm.guest_mode)
12583 nested_vmx_vmexit(vcpu, -1, 0, 0);
12584
12585 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12586 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012587 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012588 return 0;
12589}
12590
12591static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12592{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012593 struct vcpu_vmx *vmx = to_vmx(vcpu);
12594 int ret;
12595
12596 if (vmx->nested.smm.vmxon) {
12597 vmx->nested.vmxon = true;
12598 vmx->nested.smm.vmxon = false;
12599 }
12600
12601 if (vmx->nested.smm.guest_mode) {
12602 vcpu->arch.hflags &= ~HF_SMM_MASK;
12603 ret = enter_vmx_non_root_mode(vcpu, false);
12604 vcpu->arch.hflags |= HF_SMM_MASK;
12605 if (ret)
12606 return ret;
12607
12608 vmx->nested.smm.guest_mode = false;
12609 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012610 return 0;
12611}
12612
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012613static int enable_smi_window(struct kvm_vcpu *vcpu)
12614{
12615 return 0;
12616}
12617
Kees Cook404f6aa2016-08-08 16:29:06 -070012618static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012619 .cpu_has_kvm_support = cpu_has_kvm_support,
12620 .disabled_by_bios = vmx_disabled_by_bios,
12621 .hardware_setup = hardware_setup,
12622 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012623 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012624 .hardware_enable = hardware_enable,
12625 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012626 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012627 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012628
Wanpeng Lib31c1142018-03-12 04:53:04 -070012629 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070012630 .vm_alloc = vmx_vm_alloc,
12631 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070012632
Avi Kivity6aa8b732006-12-10 02:21:36 -080012633 .vcpu_create = vmx_create_vcpu,
12634 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012635 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012636
Avi Kivity04d2cc72007-09-10 18:10:54 +030012637 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012638 .vcpu_load = vmx_vcpu_load,
12639 .vcpu_put = vmx_vcpu_put,
12640
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012641 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012642 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012643 .get_msr = vmx_get_msr,
12644 .set_msr = vmx_set_msr,
12645 .get_segment_base = vmx_get_segment_base,
12646 .get_segment = vmx_get_segment,
12647 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012648 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012649 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012650 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012651 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012652 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012653 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012654 .set_cr3 = vmx_set_cr3,
12655 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012656 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012657 .get_idt = vmx_get_idt,
12658 .set_idt = vmx_set_idt,
12659 .get_gdt = vmx_get_gdt,
12660 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012661 .get_dr6 = vmx_get_dr6,
12662 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012663 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012664 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012665 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012666 .get_rflags = vmx_get_rflags,
12667 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012668
Avi Kivity6aa8b732006-12-10 02:21:36 -080012669 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012670
Avi Kivity6aa8b732006-12-10 02:21:36 -080012671 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012672 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012673 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012674 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12675 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012676 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012677 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012678 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012679 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012680 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012681 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012682 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012683 .get_nmi_mask = vmx_get_nmi_mask,
12684 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012685 .enable_nmi_window = enable_nmi_window,
12686 .enable_irq_window = enable_irq_window,
12687 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012688 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012689 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012690 .get_enable_apicv = vmx_get_enable_apicv,
12691 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012692 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012693 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012694 .hwapic_irr_update = vmx_hwapic_irr_update,
12695 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012696 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12697 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012698
Izik Eiduscbc94022007-10-25 00:29:55 +020012699 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070012700 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012701 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012702 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012703
Avi Kivity586f9602010-11-18 13:09:54 +020012704 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012705
Sheng Yang17cc3932010-01-05 19:02:27 +080012706 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012707
12708 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012709
12710 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012711 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012712
12713 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012714
12715 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012716
12717 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012718
12719 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012720
12721 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012722 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012723 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012724 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012725 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012726
12727 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012728
12729 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012730
12731 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12732 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12733 .flush_log_dirty = vmx_flush_log_dirty,
12734 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012735 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012736
Feng Wubf9f6ac2015-09-18 22:29:55 +080012737 .pre_block = vmx_pre_block,
12738 .post_block = vmx_post_block,
12739
Wei Huang25462f72015-06-19 15:45:05 +020012740 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012741
12742 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012743
12744#ifdef CONFIG_X86_64
12745 .set_hv_timer = vmx_set_hv_timer,
12746 .cancel_hv_timer = vmx_cancel_hv_timer,
12747#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012748
12749 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012750
Ladi Prosek72d7b372017-10-11 16:54:41 +020012751 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012752 .pre_enter_smm = vmx_pre_enter_smm,
12753 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012754 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012755};
12756
12757static int __init vmx_init(void)
12758{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012759 int r;
12760
12761#if IS_ENABLED(CONFIG_HYPERV)
12762 /*
12763 * Enlightened VMCS usage should be recommended and the host needs
12764 * to support eVMCS v1 or above. We can also disable eVMCS support
12765 * with module parameter.
12766 */
12767 if (enlightened_vmcs &&
12768 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
12769 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
12770 KVM_EVMCS_VERSION) {
12771 int cpu;
12772
12773 /* Check that we have assist pages on all online CPUs */
12774 for_each_online_cpu(cpu) {
12775 if (!hv_get_vp_assist_page(cpu)) {
12776 enlightened_vmcs = false;
12777 break;
12778 }
12779 }
12780
12781 if (enlightened_vmcs) {
12782 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
12783 static_branch_enable(&enable_evmcs);
12784 }
12785 } else {
12786 enlightened_vmcs = false;
12787 }
12788#endif
12789
12790 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012791 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012792 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012793 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012794
Dave Young2965faa2015-09-09 15:38:55 -070012795#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012796 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12797 crash_vmclear_local_loaded_vmcss);
12798#endif
12799
He, Qingfdef3ad2007-04-30 09:45:24 +030012800 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012801}
12802
12803static void __exit vmx_exit(void)
12804{
Dave Young2965faa2015-09-09 15:38:55 -070012805#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012806 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012807 synchronize_rcu();
12808#endif
12809
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012810 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012811
12812#if IS_ENABLED(CONFIG_HYPERV)
12813 if (static_branch_unlikely(&enable_evmcs)) {
12814 int cpu;
12815 struct hv_vp_assist_page *vp_ap;
12816 /*
12817 * Reset everything to support using non-enlightened VMCS
12818 * access later (e.g. when we reload the module with
12819 * enlightened_vmcs=0)
12820 */
12821 for_each_online_cpu(cpu) {
12822 vp_ap = hv_get_vp_assist_page(cpu);
12823
12824 if (!vp_ap)
12825 continue;
12826
12827 vp_ap->current_nested_vmcs = 0;
12828 vp_ap->enlighten_vmentry = 0;
12829 }
12830
12831 static_branch_disable(&enable_evmcs);
12832 }
12833#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080012834}
12835
12836module_init(vmx_init)
12837module_exit(vmx_exit)